Java文件讀寫詳解
讀取文件
Java 中可以使用 FileInputStream 和 FileReader 兩個(gè)類來(lái)讀取文件:
- FileInputStream 類:用于讀取二進(jìn)制文件,如圖片、視頻、音頻等文件。
- FileReader 類:用于讀取字符文件,如文本文件等。
- 使用 FileInputStream 類讀取文件
使用 FileInputStream 類讀取文件需要進(jìn)行以下步驟:
- 創(chuàng)建一個(gè) FileInputStream 對(duì)象,并指定要讀取的文件路徑。
FileInputStream fis = new FileInputStream("path/to/file");
- 使用 read() 方法讀取文件中的數(shù)據(jù)。
int data = fis.read();
代碼示例:
import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamDemo { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("src/main/resources/demo.txt"); int data; while ((data = fis.read()) != -1) { System.out.print((char) data); } fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面的代碼使用 FileInputStream 讀取了當(dāng)前項(xiàng)目中 resources 目錄下的 demo.txt 文件,并將其中的內(nèi)容輸出到控制臺(tái)上。
需要注意的是,在使用 FileInputStream 讀取文件時(shí),可能會(huì)拋出 IOException 異常,需要進(jìn)行適當(dāng)?shù)奶幚?。同時(shí),在使用完該類之后,需要調(diào)用它的 close() 方法來(lái)關(guān)閉文件輸入流。
- 使用 FileReader 類讀取文件
使用 FileReader 類讀取文件同樣需要進(jìn)行以下步驟:
- 創(chuàng)建一個(gè) FileReader 對(duì)象,并指定要讀取的文件路徑。
FileReader fr = new FileReader("path/to/file");
- 使用 read() 方法讀取文件中的數(shù)據(jù)。
int data = fr.read();
代碼示例:
import java.io.FileReader; import java.io.IOException; public class FileReaderDemo { public static void main(String[] args) { try { FileReader fr = new FileReader("src/main/resources/demo.txt"); int data; while ((data = fr.read()) != -1) { System.out.print((char) data); } fr.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面的代碼使用 FileReader 讀取了當(dāng)前項(xiàng)目中 resources 目錄下的 demo.txt 文件,并將其中的內(nèi)容輸出到控制臺(tái)上。
需要注意的是,與 FileInputStream 類似,在使用 FileReader 讀取文件時(shí)也可能會(huì)拋出 IOException 異常,需要進(jìn)行適當(dāng)?shù)奶幚?。同時(shí),在使用完該類之后,也需要調(diào)用它的 close() 方法來(lái)關(guān)閉文件輸入流。
寫入文件
在 Java 中,可以使用 FileOutputStream 和 FileWriter 兩個(gè)類來(lái)寫入文件:
- FileOutputStream 類:用于寫入二進(jìn)制文件,如圖片、視頻、音頻等文件。
- FileWriter 類:用于寫入字符文件,如文本文件等。
- 使用 FileOutputStream 類寫入文件
使用 FileOutputStream 類寫入文件需要進(jìn)行以下步驟:
- 創(chuàng)建一個(gè) FileOutputStream 對(duì)象,并指定要寫入的文件路徑。
FileOutputStream fos = new FileOutputStream("path/to/file");
- 使用 write() 方法將數(shù)據(jù)寫入到文件中。
fos.write(data);
代碼示例:
import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamDemo { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("src/main/resources/demo.txt"); String data = "Hello, World!"; byte[] bytes = data.getBytes(); fos.write(bytes); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面的代碼使用 FileOutputStream 將字符串 "Hello, World!" 寫入到當(dāng)前項(xiàng)目中 resources 目錄下的 demo.txt 文件中。
需要注意的是,在使用 FileOutputStream 寫入文件時(shí),可能會(huì)拋出 IOException 異常,需要進(jìn)行適當(dāng)?shù)奶幚?。同時(shí),在使用完該類之后,需要調(diào)用它的 close() 方法來(lái)關(guān)閉文件輸出流。
- 使用 FileWriter 類寫入文件
使用 FileWriter 類寫入文件同樣需要進(jìn)行以下步驟:
- 創(chuàng)建一個(gè) FileWriter 對(duì)象,并指定要寫入的文件路徑。
FileWriter fw = new FileWriter("path/to/file");
- 使用 write() 方法將數(shù)據(jù)寫入到文件中。
fw.write(data);
代碼示例:
import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try { FileWriter fw = new FileWriter("src/main/resources/demo.txt"); String data = "Hello, World!"; fw.write(data); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面的代碼使用 FileWriter 將字符串 "Hello, World!" 寫入到當(dāng)前項(xiàng)目中 resources 目錄下的 demo.txt 文件中。
需要注意的是,與 FileOutputStream 類似,在使用 FileWriter 寫入文件時(shí)也可能會(huì)拋出 IOException 異常,需要進(jìn)行適當(dāng)?shù)奶幚?。同時(shí),在使用完該類之后,也需要調(diào)用它的 close() 方法來(lái)關(guān)閉文件輸出流。
復(fù)制文件
在 Java 中,可以通過(guò) FileInputStream 和 FileOutputStream 來(lái)實(shí)現(xiàn)文件復(fù)制操作。具體步驟如下:
- 創(chuàng)建一個(gè) FileInputStream 對(duì)象,并指定要復(fù)制的源文件路徑。
FileInputStream fis = new FileInputStream("path/to/source/file");
- 創(chuàng)建一個(gè) FileOutputStream 對(duì)象,并指定要寫入目標(biāo)文件的路徑。
FileOutputStream fos = new FileOutputStream("path/to/target/file");
- 建立緩沖區(qū),從輸入流中讀取數(shù)據(jù)并寫入到輸出流中。
byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); }
- 關(guān)閉 FileInputStream 和 FileOutputStream 流。
fis.close(); fos.close();
代碼示例:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileDemo { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("src/main/resources/demo.txt"); FileOutputStream fos = new FileOutputStream("src/main/resources/demo_copy.txt"); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
上面的代碼實(shí)現(xiàn)了將當(dāng)前項(xiàng)目中 resources 目錄下的 demo.txt 文件復(fù)制到 demo_copy.txt 文件中。
需要注意的是,在使用完相關(guān)流之后,需要調(diào)用它們的 close() 方法來(lái)關(guān)閉流資源,以釋放內(nèi)存。
刪除文件
在 Java 中,可以使用 File 類的 delete() 方法來(lái)刪除文件。具體步驟如下:
- 創(chuàng)建一個(gè) File 對(duì)象,并指定要?jiǎng)h除的文件路徑。
File file = new File("path/to/file");
- 調(diào)用 delete() 方法來(lái)刪除文件。
file.delete();
代碼示例:
import java.io.File; public class DeleteFileDemo { public static void main(String[] args) { File file = new File("src/main/resources/demo_copy.txt"); if (file.delete()) { System.out.println(file.getName() + " is deleted!"); } else { System.out.println("Delete operation is failed."); } } }
上面的代碼刪除了當(dāng)前項(xiàng)目中 resources 目錄下的 demo_copy.txt 文件。
需要注意的是,在使用 delete() 方法刪除文件時(shí),可能會(huì)拋出 SecurityException 異常,需要進(jìn)行適當(dāng)?shù)奶幚怼M瑫r(shí),在刪除文件之前,需要判斷該文件是否存在,可以使用 File 類的 exists() 方法來(lái)實(shí)現(xiàn)。
總結(jié)
本篇文章詳細(xì)介紹了 Java 文件讀寫的相關(guān)知識(shí),包括讀取文件、寫入文件、復(fù)制文件和刪除文件等操作。希望能夠?qū)Υ蠹业膶W(xué)習(xí)和實(shí)踐有所幫助。需要注意的是,在進(jìn)行文件讀寫操作時(shí),要注意異常的處理,以保證代碼的健壯性。同時(shí),在使用完相關(guān)流之后,也需要調(diào)用 close() 方法來(lái)關(guān)閉流資源,以釋放內(nèi)存。
以上就是Java文件讀寫詳解的詳細(xì)內(nèi)容,更多關(guān)于Java文件讀寫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于@RequestParam與@RequestBody使用對(duì)比
這篇文章主要介紹了@RequestParam與@RequestBody的使用對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲(chǔ)
本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲(chǔ),接下來(lái)這篇我們將圍繞MongoDB進(jìn)行,MongoDB是一個(gè)開源的,面向文檔的NoSQL數(shù)據(jù)庫(kù)管理系統(tǒng),使用類似JSON的BSON(二進(jìn)制JSON)格式來(lái)存儲(chǔ)數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強(qiáng)大的查詢功能,需要的朋友可以參考下2023-11-11Spring Security 表單登錄功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Security 表單登錄,本文將構(gòu)建在之前簡(jiǎn)單的 Spring MVC示例 之上,因?yàn)檫@是設(shè)置Web應(yīng)用程序和登錄機(jī)制的必不可少的。需要的朋友可以參考下2019-06-06Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法
這篇文章主要給大家介紹了關(guān)于Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法,通過(guò)文中介紹的方法可以將時(shí)間分成我們想要的時(shí)間段,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2021-07-07