Ⅰ java怎麼讀取properties文件
Java讀取properties文件的步驟如下:
創建Properties類對象:
取得properties文件的輸入流:
使用Properties類載入該輸入流內容:
關閉輸入流節約資源:
操作Properties對象取得文件中的內容:
Properties類的一些關鍵特性:
通過這些步驟和特性,Java程序可以方便地讀取、操作以及保存properties文件。
Ⅱ Java讀取文件內容(Java讀取文件內容為字元串)
JAVA中讀取文件(二進制,字元)內容的幾種方JAVA中讀取文件內容的方法有很多,比如按位元組讀取文件內容,按字元讀取文件內容,按行讀取文件內容,隨機讀取文件內容等方法,本文就以上方法的具體實現給出代碼,需要的可以直接復制使用
publicclassReadFromFile{
/**
*以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
//一次讀一個位元組
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
//一次讀多個位元組
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
//一次讀一個字元
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//對於windows下, 這兩個字元在一起時,表示一個換行。
//但如果這兩個字元分開顯示時,會換兩次行。
//因此,屏蔽掉 ,或者屏蔽 。否則,將會多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
//一次讀多個字元
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//讀入多個字元到字元數組中,charread為一次讀取字元數
while((charread=reader.read(tempchars))!=-1){
//同樣屏蔽掉 不顯示
if((charread==tempchars.length)
(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;icharread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以行為單位讀取文件,常用於讀面向行的格式化文件
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次讀入一行,直到讀入null為文件結束
while((tempString=reader.readLine())!=null){
//顯示行號
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*隨機讀取文件內容
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("隨機讀取一段文件內容:");
//打開一個隨機訪問文件流,按只讀方式
randomFile=newRandomAccessFile(fileName,"r");
//文件長度,位元組數
longfileLength=randomFile.length();
//讀文件的起始位置
intbeginIndex=(fileLength4)?4:0;
//將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
//將一次讀取的位元組數賦給byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}
/**
*顯示輸入流中還剩的位元組數
*/
(InputStreamin){
try{
System.out.println("當前位元組輸入流中的位元組數為:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
Java如何讀取txt文件的內容?java讀取txt文件內容。可以作如下理解:
首先獲得一個文件句柄。Filefile=newFile();file即為文件句柄。兩人之間連通電話網路了。接下來可以開始打電話了。
通過這條線路讀取甲方的信息:newFileInputStream(file)目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西
既然你使用了FileInputStream()。那麼對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據
解讀完成後要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用位元組碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。
package?com.campu;
?
import?java.io.BufferedInputStream;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.InputStreamReader;
import?java.io.Reader;
?
/**
?*?@author?Java團長
?*?H20121012.java
?*?2017-10-29上午11:22:21
?*/
public?class?H20121012?{
????/**
?????*?功能:Java讀取txt文件的內容
?????*?步驟:1:先獲得文件句柄
?????*?2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
?????*?3:讀取到輸入流後,需要讀取生成位元組流
?????*?4:一行一行的輸出。readline()。
?????*?備註:需要考慮的是異常情況
?????*?@param?filePath
?????*/
????public?static?void?readTxtFile(String?filePath){
????????try?{
????????????????String?encoding="GBK";
????????????????File?file=new?File(filePath);
????????????????if(file.isFile()??file.exists()){?//判斷文件是否存在
????????????????????InputStreamReader?read?=?new?InputStreamReader(
????????????????????new?FileInputStream(file),encoding);//考慮到編碼格式
????????????????????BufferedReader?bufferedReader?=?new?BufferedReader(read);
????????????????????String?lineTxt?=?null;
????????????????????while((lineTxt?=?bufferedReader.readLine())?!=?null){
????????????????????????System.out.println(lineTxt);
????????????????????}
????????????????????read.close();
????????}else{
????????????System.out.println("找不到指定的文件");
????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("讀取文件內容出錯");
????????????e.printStackTrace();
????????}
?????
????}
?????
????public?static?void?main(String?argv[]){
????????String?filePath?=?"L:\Apache\htdocs\res\20121012.txt";
//??????"res/";
????????readTxtFile(filePath);
????}
?????
?????
?
}
我有一個微信公眾號,經常會分享一些Java技術相關的干貨文章,還有一些學習資源。
如果你需要的話,可以用微信搜索「Java團長」或者「javatuanzhang」關注。
java中怎樣從文件中讀取數據?
分為讀位元組,讀字元兩種讀法x0dx0a◎◎◎FileInputStream位元組輸入流讀文件◎◎◎x0dx0apublicclassMaintest{(String[]args)throwsIOException{x0dx0ax0dx0aFilef=newFile("G:\justforfun\xiangwei.txt");=newFileInputStream(f);x0dx0ax0dx0abyte[]bs=newbyte[1024];x0dx0ax0dx0aintcount=0;x0dx0awhile((count=fin.read(bs))0)x0dx0a{x0dx0ax0dx0aStringstr=newString(bs,0,count);//反復定義新變數:每一次都重新定義新變數,接收新讀取的數據x0dx0ax0dx0aSystem.out.println(str);//反復輸出新變數:每一次都輸出重新定義的新變數x0dx0a}x0dx0afin.close();x0dx0a}x0dx0a}x0dx0ax0dx0a◎◎◎FileReader字元輸入流讀文件◎◎◎x0dx0apublicclassMaintest{x0dx0apublicstaticvoidmain(String[]args)throwsIOException{x0dx0ax0dx0aFilef=newFile("H:\justforfun\xiangwei.txt");x0dx0ax0dx0aFileReaderfre=newFileReader(f);x0dx0ax0dx0aBufferedReaderbre=newBufferedReader(fre);x0dx0ax0dx0aStringstr="";x0dx0awhile((str=bre.readLine())!=null)//●判斷最後一行不存在,為空x0dx0a{x0dx0aSystem.out.println(str);x0dx0a}x0dx0abre.close();x0dx0afre.close();x0dx0ax0dx0a}x0dx0ax0dx0a}
java中在怎麼讀取文件夾中的內容以下java程序的作用是將當前目錄及其子目錄中的.java文件收集到collection.txt文件中,並添加行號,你可以參考一下。
importjava.io.*;
publicclassCollection
{
publicstaticvoidmain(String[]args)throwsException
{
finalStringF=".\collection.txt";
FW=newFileWriter(newFile(F));
Collection.ProcessDirectory(newFile("."));
Collection.FW.flush();
Collection.FW.close();
}
(Filed)throwsException
{
File[]ds=null;
Collection.ProcessJavaFile(d);
ds=d.listFiles(Collection.DFilter);
for(inti=0;ids.length;i++)
{
Collection.ProcessDirectory(ds[i]);
}
}
(Filed)throwsException
{
Stringline=null;
LineNumberReaderlnr=null;
File[]fs=d.listFiles(Collection.FNFilter);
for(inti=0;ifs.length;i++)
{
lnr=newLineNumberReader(newFileReader(fs[i]));
Collection.FW.write(fs[i].getCanonicalPath()+" ");
System.out.println(fs[i].getCanonicalPath());
while(null!=(line=lnr.readLine()))
{
Collection.FW.write(""+lnr.getLineNumber()+""+line+" ");
System.out.println(""+lnr.getLineNumber()+""+line);
}
Collection.FW.write(" ");
System.out.println();
}
}
privatestaticFileWriterFW;
=newFilenameFilter()
{
publicbooleanaccept(Filedir,Stringname)
{
returnname.endsWith(".java");
}
};
=newFileFilter()
{
publicbooleanaccept(Filepathname)
{
returnpathname.isDirectory();
}
};
}
java中怎樣從一個文件中讀取文件信息java讀取文件路徑、所佔空間大小等文件消息,主要是使用FileInputStream類來操作,示例如下:
import?java.io.File;
Ⅲ java中獲取文件路徑的幾種方式
獲取當前類的所在工程路徑;如果未添加「/」,則代碼如下:
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
執行結果為:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;第二種方法為:
File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
執行結果為:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;第三種方法為:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
執行結果為:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑;第四種方法為:
System.out.println(System.getProperty("user.dir"));
執行結果為:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑;第五種方法為:
System.out.println(System.getProperty("java.class.path"));
執行結果為:C:\Documents and Settings\Administrator\workspace\projectName\bin
以上介紹了五種獲取文件路徑的方法,每種方法都有其特點和適用場景。第一種方法適用於需要獲取類所在目錄的路徑,但結果包含bin文件夾;第二種方法適用於獲取文件系統中的絕對路徑;第三種方法適用於獲取類載入器資源的URL路徑,結果包含文件協議;第四種方法獲取當前工作目錄,即工程根目錄;第五種方法獲取類路徑,通常指向編譯後的類文件所在的目錄。
在實際開發中,根據具體需求選擇合適的方法。例如,如果需要獲取源代碼文件的路徑,可以使用第三種方法;如果需要獲取編譯後的類文件路徑,則使用第五種方法更為合適。
需要注意的是,路徑格式在Windows和Linux系統中可能存在差異,因此在跨平台項目中應謹慎使用這些方法。同時,建議在編寫代碼時考慮路徑的可讀性和安全性,避免硬編碼路徑。
在處理文件路徑時,務必考慮文件系統的限制和特殊字元,確保路徑的正確性和兼容性。此外,對於敏感文件和目錄,應採取適當的訪問控制措施,以防止意外訪問或修改。