Java使用字節(jié)流、緩沖流、轉(zhuǎn)換流與對(duì)象流讀寫文件的示例代碼
一、Java 中有哪些流

二、Java 中流的分類
- 節(jié)點(diǎn)流(從文件內(nèi)容到流的輸入輸出)
- 字節(jié)流 FileInputStream FileOutputStream
- 字符流 FileReader FileWriter
- 處理流(基于節(jié)點(diǎn)流的進(jìn)一步封裝)
- 緩沖流 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
- 轉(zhuǎn)換流 DataInputStream DataOutputStream
- 對(duì)象流 ObjectInputStream ObjectOutputStream
三、Java 中流如何使用
- 字節(jié)流
- 可以讀寫文本、圖片、音頻、視頻等各種類型的文件。
- 比如 world、png、mp4 等。
- 字符流
- 只可以讀寫文本文件
- 比如 txt、java、py、c 等。
- 注意 world excel ppt 不屬于文本文件。
- 緩沖流
- 作用:先把數(shù)據(jù)緩存到內(nèi)存中,然后批量寫入磁盤中,減少跟磁盤交互的次數(shù),可以提高讀寫文件的效率。
- 如果不使用緩沖流,讀一次寫一次跟磁盤交互一次,跟磁盤交互次數(shù)多,讀寫效率較低。
- 轉(zhuǎn)換流
- 用于文件內(nèi)容的編碼和解碼,文件內(nèi)容中保存的是二進(jìn)制數(shù)據(jù),解碼后,我們才可以看懂。
- 解碼,把二進(jìn)制數(shù)據(jù)按照指定的字符集轉(zhuǎn)換為字符。
- 編碼,把字符按照指定字符集編碼為二進(jìn)制數(shù)據(jù)。
- 對(duì)象流
- 用于把對(duì)象序列化為二進(jìn)制數(shù)據(jù),以便持久化到磁盤或使用網(wǎng)絡(luò)傳輸。
- 反序列化,把二進(jìn)制數(shù)據(jù)反序列化為內(nèi)存中的 Java 對(duì)象。
四、使用字節(jié)流讀寫文件


public void copyFileWithFile(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//1.
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2.
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//3. 讀寫過程
int len;
byte[] buffer = new byte[100];
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 關(guān)閉資源
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
五、使用緩沖流讀寫文件

public void copyFileWithBuffered(String srcPath, String destPath) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2.
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3. 讀寫過程
int len;
byte[] buffer = new byte[100];
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 關(guān)閉資源(1. 需要先關(guān)閉緩沖流,再關(guān)閉文件流 2. 默認(rèn)情況下,關(guān)閉外層流時(shí),也會(huì)自動(dòng)關(guān)閉內(nèi)部的流)
try {
if(bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
//可以省略
// fos.close();
// fis.close();
}
}


六、字符流只能讀寫文本文件
public void test() {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
isr = new InputStreamReader(new FileInputStream("康師傅的話.txt"),"gbk");
osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\shkstart\\Desktop\\寄語.txt"),"utf-8");
char[] cbuf = new char[1024];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
osw.flush();
}
System.out.println("文件復(fù)制完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
七、使用轉(zhuǎn)換流進(jìn)行解碼編碼
使用 UTF8 解碼文件得到文件內(nèi)容后使用 GBK 編碼文件輸出到新的文件中
public void test4() {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1. 造文件
File file1 = new File("dbcp_gbk.txt");
File file2 = new File("dbcp_gbk_to_utf8.txt");
//2. 造流
FileInputStream fis = new FileInputStream(file1);
//參數(shù)2對(duì)應(yīng)的是解碼集,必須與dbcp_gbk.txt的編碼集一致。
isr = new InputStreamReader(fis,"GBK");
FileOutputStream fos = new FileOutputStream(file2);
//參數(shù)2指明內(nèi)存中的字符存儲(chǔ)到文件中的字節(jié)過程中使用的編碼集。
osw = new OutputStreamWriter(fos,"utf8");
//3. 讀寫過程
char[] cBuffer = new char[1024];
int len;
while((len = isr.read(cBuffer)) != -1){
osw.write(cBuffer,0,len);
}
System.out.println("操作完成");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//4. 關(guān)閉資源
try {
if(osw != null)
osw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if(isr != null)
isr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
八、使用對(duì)象流序列化反序列化


person 對(duì)象
package com.atguigu05.objectstream;
import java.io.Serializable;
public class Person implements Serializable { //Serializable:屬于一個(gè)標(biāo)識(shí)接口
String name;
int age;
int id;
Account acct;
static final long serialVersionUID = 422334254234L;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public Person(String name, int age, int id, Account acct) {
this.name = name;
this.age = age;
this.id = id;
this.acct = acct;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", acct=" + acct +
'}';
}
}
class Account implements Serializable{
double balance;
static final long serialVersionUID = 422234L;
public Account(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"balance=" + balance +
'}';
}
}
序列化 person 對(duì)象
public void test3(){
ObjectOutputStream oos = null;
try {
File file = new File("object1.dat");
oos = new ObjectOutputStream(new FileOutputStream(file));
//2.寫出數(shù)據(jù)即為序列化的過程
Person p1 = new Person("Tom",12);
oos.writeObject(p1);
oos.flush();
Person p2 = new Person("Jerry",23,1001,new Account(2000));
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(oos != null)
oos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
反序列化 person 對(duì)象
public void test4() {
ObjectInputStream ois = null;
try {
File file = new File("object1.dat");
ois = new ObjectInputStream(new FileInputStream(file));
//2. 讀取文件中的對(duì)象(或反序列化的過程)
Person person = (Person) ois.readObject();
System.out.println(person);
Person person1 = (Person) ois.readObject();
System.out.println(person1);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
try {
if(ois != null)
ois.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
以上就是Java使用字節(jié)流、緩沖流、轉(zhuǎn)換流與對(duì)象流讀寫文件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java讀寫文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot整合XXLJob的實(shí)現(xiàn)示例
XXLJob是大眾點(diǎn)評(píng)開源的分布式任務(wù)調(diào)度平臺(tái),支持Cron、固定間隔等觸發(fā)方式,本文主要介紹了SpringBoot整合XXLJob的實(shí)現(xiàn)示例,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
java實(shí)現(xiàn)浮點(diǎn)數(shù)轉(zhuǎn)人民幣的小例子
java實(shí)現(xiàn)浮點(diǎn)數(shù)轉(zhuǎn)人民幣的小例子,需要的朋友可以參考一下2013-03-03
spring?boot對(duì)敏感信息進(jìn)行加解密的項(xiàng)目實(shí)現(xiàn)
本文主要介紹了spring?boot對(duì)敏感信息進(jìn)行加解密的項(xiàng)目實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
IDEA連接MySQL數(shù)據(jù)庫的4種方法圖文教程
IDEA是一種流行的Java開發(fā)工具,可以方便地連接MySQL,這篇文章主要給大家介紹了關(guān)于IDEA連接MySQL數(shù)據(jù)庫的4種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
spring學(xué)習(xí)之util:properties的使用
這篇文章主要介紹了spring學(xué)習(xí)之util:properties的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
Spring Boot Actuator 端點(diǎn)的未授權(quán)訪問漏洞是一個(gè)安全性問題,可能會(huì)導(dǎo)致未經(jīng)授權(quán)的用戶訪問敏感的應(yīng)用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09
10個(gè)Java解決內(nèi)存溢出OOM的方法詳解
在Java開發(fā)過程中,有效的內(nèi)存管理是保證應(yīng)用程序穩(wěn)定性和性能的關(guān)鍵,不正確的內(nèi)存使用可能導(dǎo)致內(nèi)存泄露甚至是致命的OutOfMemoryError(OOM),下面我們就來學(xué)習(xí)一下有哪些解決辦法吧2024-01-01

