導航:首頁 > 編程語言 > java獲取文件流

java獲取文件流

發布時間:2025-07-18 10:45:56

java怎麼讀取properties文件

Java讀取properties文件的步驟如下

  1. 創建Properties類對象

    • 使用Properties類創建一個對象,該對象是用於操作屬性文件的。
  2. 取得properties文件的輸入流

    • 通過ClassLoader或其他方式獲取到properties文件的輸入流。
  3. 使用Properties類載入該輸入流內容

    • 調用Properties對象的load方法,將輸入流中的內容載入到Properties對象中。
  4. 關閉輸入流節約資源

    • 載入完成後,關閉輸入流以釋放系統資源。
  5. 操作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系統中可能存在差異,因此在跨平台項目中應謹慎使用這些方法。同時,建議在編寫代碼時考慮路徑的可讀性和安全性,避免硬編碼路徑。

在處理文件路徑時,務必考慮文件系統的限制和特殊字元,確保路徑的正確性和兼容性。此外,對於敏感文件和目錄,應採取適當的訪問控制措施,以防止意外訪問或修改。

閱讀全文

與java獲取文件流相關的資料

熱點內容
短地址伺服器搭建 瀏覽:828
單片機求和1 瀏覽:797
群暉加密共享文件夾數據恢復 瀏覽:661
單片機匯編程序教程 瀏覽:615
數控編程dnc 瀏覽:543
程序員來錢快嗎 瀏覽:373
phpmssql編碼 瀏覽:536
jenkins集成php 瀏覽:515
androidcpu控制 瀏覽:95
mt修改器是反編譯嗎 瀏覽:449
有什麼app都有手機卡 瀏覽:809
解壓按摩用工具 瀏覽:545
獵豹螺桿壓縮機廠家密碼 瀏覽:504
編譯出現異常處理方案 瀏覽:528
stata的安裝命令 瀏覽:397
觸摸屏單片機驅動 瀏覽:965
自製文件夾圖標要保存為什麼格式 瀏覽:883
單片機管角分布時的復用功能 瀏覽:966
dev編譯出現亂碼怎麼解決 瀏覽:665
西安JAVA程序員薪資水平 瀏覽:730