Java?IO流總結(jié)
基礎(chǔ)流
1、IO概述

1.1 什么是IO
數(shù)據(jù)的傳輸可以看做是一種數(shù)據(jù)的流動(dòng),按照流動(dòng)的方向,以內(nèi)存為基準(zhǔn),分為輸入input 和輸出output ,即流向內(nèi)存是輸入流,流出內(nèi)存的輸出流。
Java中I/O操作主要是指使用java.io包下的內(nèi)容,進(jìn)行輸入、輸出操作。輸入也叫做讀取數(shù)據(jù),輸出也叫做作寫出數(shù)據(jù)。
1.2 IO的分類
根據(jù)數(shù)據(jù)的流向分為:輸入流和輸出流。
- 輸入流 :把數(shù)據(jù)從
其他設(shè)備上讀取到內(nèi)存中的流。 - 輸出流 :把數(shù)據(jù)從
內(nèi)存中寫出到其他設(shè)備上的流。
格局?jǐn)?shù)據(jù)的類型分為:字節(jié)流和字符流。
- 字節(jié)流 :以字節(jié)為單位,讀寫數(shù)據(jù)的流。
- 字符流 :以字符為單位,讀寫數(shù)據(jù)的流。
輸入:硬盤–>內(nèi)存
輸出:內(nèi)存–>硬盤
1.3 頂級(jí)父類們
| 輸入流 | 輸出流 | |
|---|---|---|
| 字節(jié)流 | 字節(jié)輸入流InputStream | 字節(jié)輸出流OutputStream |
| 字符流 | 字符輸入流Reader | 字符輸出流Writer |
一個(gè)簡單的方法判斷文件存儲(chǔ)是否是字符流:將文件使用Windows自帶的文本編輯器打開,能直接看懂的是字符流,不能直接看懂的(比如亂碼)是字節(jié)流。
2、字節(jié)流
字節(jié)流讀取文件的時(shí)候,文件中不要有中文。
2.1 一切皆為字節(jié)
一切文件數(shù)據(jù)(文本、圖片、視頻等)在存儲(chǔ)時(shí),都是以二進(jìn)制數(shù)字的形式保存,都一個(gè)一個(gè)的字節(jié),那么傳輸時(shí)一樣如此。所以,字節(jié)流可以傳輸任意文件數(shù)據(jù)。
2.2 字節(jié)輸出流 OutputStream
java.io.OutputStream 抽象類是表示字節(jié)輸出流的所有類的超類,將指定的字節(jié)信息寫出到目的地。它定義了字節(jié)輸出流的基本共性功能方法。
public void close():關(guān)閉此輸出流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。public void flush():刷新此輸出流并強(qiáng)制任何緩沖的輸出字節(jié)被寫出。public void write(byte[] b):將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流。public void write(byte[] b, int off, int len):從指定的字節(jié)數(shù)組寫入 len字節(jié),從偏移量 off開始輸出到此輸出流。public abstract void write(int b):將指定的字節(jié)輸出流。
對(duì)于close方法:當(dāng)完成流的操作時(shí),必須調(diào)用此方法,釋放系統(tǒng)資源。
2.3 FileOutputStream類
OutputStream有很多子類,從最簡單的一個(gè)子類開始。java.io.FileOutputStream 類是文件輸出流,用于將數(shù)據(jù)寫出到文件。
2.3.1 構(gòu)造方法
public FileOutputStream(File file):創(chuàng)建文件輸出流以寫入由指定的 File對(duì)象表示的文件。public FileOutputStream(String name): 創(chuàng)建文件輸出流以指定的名稱寫入文件。
當(dāng)你創(chuàng)建一個(gè)流對(duì)象時(shí),必須傳入一個(gè)文件路徑。該路徑下,如果沒有這個(gè)文件,會(huì)創(chuàng)建該文件。如果有這個(gè)文件,會(huì)清空這個(gè)文件的數(shù)據(jù)。
代碼示例:
public class FileOutputStreamConstructor throws IOException {
public static void main(String[] args) {
// 使用File對(duì)象創(chuàng)建流對(duì)象
File file = new File("a.txt");
FileOutputStream fos = new FileOutputStream(file);
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("b.txt");
}
}2.3.2 寫出字節(jié)數(shù)據(jù)
(1)寫出字節(jié):write(int b) 方法,每次可以寫出一個(gè)字節(jié)數(shù)據(jù),代碼使用演示:
實(shí)現(xiàn)需求:寫出一段文字到本地文件中。(暫時(shí)不寫中文)
實(shí)現(xiàn)步驟:
創(chuàng)建對(duì)象
寫出數(shù)據(jù)
釋放資源public class FileOutputStreamDemo01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
//寫出 輸出流 OutputStream
//本地文件 File
FileOutputStream fos = new FileOutputStream("my-io\\a.txt");
//2.寫出數(shù)據(jù)
fos.write(97);
//3.釋放資源
fos.close();
}
}
注:
- 雖然參數(shù)為int類型四個(gè)字節(jié),但是只會(huì)保留一個(gè)字節(jié)的信息寫出。
- 流操作完畢后,必須釋放系統(tǒng)資源,調(diào)用close方法,千萬記得。
- 創(chuàng)建字節(jié)輸出流對(duì)象
- 參數(shù)是字符串表示的路徑或者是File對(duì)象都是可以的
- 如果文件不存在會(huì)創(chuàng)建一個(gè)新的文件,但是要保證父級(jí)路徑是存在的。
- 如果文件已經(jīng)存在,則會(huì)清空文件
- 寫數(shù)據(jù)
- write方法的參數(shù)是整數(shù),但是實(shí)際上寫到本地文件中的是整數(shù)在ASCII上對(duì)應(yīng)的字符
- ‘9’
- '7’
(2)寫出字節(jié)數(shù)組:write(byte[] b),每次可以寫出數(shù)組中的數(shù)據(jù),代碼使用演示:
void write(byte[] b):一次寫一個(gè)字節(jié)數(shù)組數(shù)據(jù)
public class FileOutputStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
FileOutputStream fos = new FileOutputStream("my-io\\a.txt");
//2.一次寫一個(gè)字節(jié)數(shù)組數(shù)據(jù)
byte[] bytes = {97, 98, 99, 100, 101};
fos.write(bytes);
//4.釋放資源
fos.close();
}
}
(3)寫出指定長度字節(jié)數(shù)組:write(byte[] b, int off, int len) ,每次寫出從off索引開始,len個(gè)字節(jié),代碼使用演示:
void write(byte[] b, int off, int len) 一次寫一個(gè)字節(jié)數(shù)組的部分?jǐn)?shù)據(jù) 參數(shù)一:數(shù)組 參數(shù)二:起始索引 參數(shù)三:個(gè)數(shù)
public class FileOutputStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
FileOutputStream fos = new FileOutputStream("my-io\\a.txt");
//2.一次寫一個(gè)字節(jié)數(shù)組數(shù)據(jù)
byte[] bytes = {97, 98, 99, 100, 101};
//3.一次寫一個(gè)字節(jié)數(shù)組的部分?jǐn)?shù)據(jù)
fos.write(bytes,1,2); // b c
//4.釋放資源
fos.close();
}
}
2.3.3 數(shù)據(jù)追加續(xù)寫
經(jīng)過以上的演示,每次程序運(yùn)行,創(chuàng)建輸出流對(duì)象,都會(huì)清空目標(biāo)文件中的數(shù)據(jù)。需要在構(gòu)造方法的參數(shù)傳入一個(gè)boolean類型的值,true 表示追加數(shù)據(jù),false 表示清空原有數(shù)據(jù)。這樣創(chuàng)建的輸出流對(duì)象,就可以指定是否追加續(xù)寫了。
public FileOutputStream(File file, boolean append): 創(chuàng)建文件輸出流以寫入由指定的 File對(duì)象表示的文件。public FileOutputStream(String name, boolean append): 創(chuàng)建文件輸出流以指定的名稱寫入文件。
代碼示例:
public class FileOutputStreamDemo04 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象,開啟續(xù)寫
FileOutputStream fos = new FileOutputStream("my-io\\a.txt",true);
//2.寫出數(shù)據(jù)
String str = "Hello";
byte[] bytes = str.getBytes();
fos.write(bytes);
//3.釋放資源
fos.close();
}
}
2.3.4 寫出換行
Windows系統(tǒng)里,換行符號(hào)是\r\n
public class FileOutputStreamDemo04 {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("my-io\\a.txt");
// 定義字節(jié)數(shù)組
byte[] words = {97,98,99,100,101};
// 遍歷數(shù)組
for (int i = 0; i < words.length; i++) {
// 寫出一個(gè)字節(jié)
fos.write(words[i]);
// 寫出一個(gè)換行, 換行符號(hào)轉(zhuǎn)成數(shù)組寫出
fos.write("\r\n".getBytes());
}
// 關(guān)閉資源
fos.close();
}
}
- 回車符
\r和換行符\n:
- 回車符:回到一行的開頭(return)。
- 換行符:下一行(newline)。
- 系統(tǒng)中的換行:
- Windows系統(tǒng)里,每行結(jié)尾是
回車+換行,即\r\n;- Unix系統(tǒng)里,每行結(jié)尾只有
換行,即\n;- Mac系統(tǒng)里,每行結(jié)尾是
回車,即\r。從 Mac OS X開始與Linux統(tǒng)一。
2.4 字節(jié)輸入流 InputStream
java.io.InputStream 抽象類是表示字節(jié)輸入流的所有類的超類,可以讀取字節(jié)信息到內(nèi)存中。它定義了字節(jié)輸入流的基本共性功能方法。
public void close():關(guān)閉此輸入流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。public abstract int read(): 從輸入流讀取數(shù)據(jù)的下一個(gè)字節(jié)。public int read(byte[] b): 從輸入流中讀取一些字節(jié)數(shù),并將它們存儲(chǔ)到字節(jié)數(shù)組 b中 。
close方法,當(dāng)完成流的操作時(shí),必須調(diào)用此方法,釋放系統(tǒng)資源。
2.5 FileInputStream類
java.io.FileInputStream 類是文件輸入流,從文件中讀取字節(jié)。
2.5.1 構(gòu)造方法
創(chuàng)建一個(gè)流對(duì)象時(shí),必須傳入一個(gè)文件路徑。該路徑下,如果沒有該文件,會(huì)拋出FileNotFoundException 。
FileInputStream(File file): 通過打開與實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream ,該文件由文件系統(tǒng)中的 File對(duì)象 file命名。FileInputStream(String name): 通過打開與實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream ,該文件由文件系統(tǒng)中的路徑名 name命名。
代碼示例:
public class FileInputStreamConstructor throws IOException{
public static void main(String[] args) {
// 使用File對(duì)象創(chuàng)建流對(duì)象
File file = new File("a.txt");
FileInputStream fos = new FileInputStream(file);
// 使用文件名稱創(chuàng)建流對(duì)象
FileInputStream fos = new FileInputStream("b.txt");
}
}2.5.2 讀取字節(jié)數(shù)據(jù)
(1)讀取字節(jié):read方法,每次可以讀取一個(gè)字節(jié)的數(shù)據(jù),提升為int類型,讀取到文件末尾,返回-1,代碼使用演示:
public class FileInputStreamDemo01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
FileInputStream fis = new FileInputStream("my-io\\a.txt");
//2.讀取數(shù)據(jù),返回一個(gè)字節(jié)
int read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
// 讀取到末尾,返回-1
read = fis.read();
System.out.println( read);
//3.關(guān)閉資源
fis.close();
}
}

循環(huán)改進(jìn)讀取方式,代碼使用演示:
public class FileInputStreamDemo03 {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileInputStream fis = new FileInputStream("my-io\\a.txt");
// 定義變量,保存數(shù)據(jù)
int b;
// 循環(huán)讀取
while ((b = fis.read()) != -1) {
System.out.println((char) b);
}
// 關(guān)閉資源
fis.close();
}
}
- 雖然讀取了一個(gè)字節(jié),但是會(huì)自動(dòng)提升為int類型。
- 流操作完畢后,必須釋放系統(tǒng)資源,調(diào)用close方法,千萬記得。
(2)使用字節(jié)數(shù)組讀取:read(byte[] b),每次讀取b的長度個(gè)字節(jié)到數(shù)組中,返回讀取到的有效字節(jié)個(gè)數(shù),讀取到末尾時(shí),返回-1 ,代碼使用演示:
public class FileInputStreamDemo05 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
FileInputStream fis = new FileInputStream("my-io\\a.txt");
//2.讀取數(shù)據(jù)
byte[] bytes = new byte[2];
//一次讀取多個(gè)字節(jié)數(shù)據(jù),具體讀多少,跟數(shù)組的長度有關(guān)
//返回值:本次讀取到了多少個(gè)字節(jié)數(shù)據(jù)
int len1 = fis.read(bytes);
System.out.println(len1);//2
String str1 = new String(bytes,0,len1);
System.out.println(str1);//ab
int len2 = fis.read(bytes);
System.out.println(len2);//2
String str2 = new String(bytes,0,len2);
System.out.println(str2);//cd
int len3 = fis.read(bytes);
System.out.println(len3);// 1
String str3 = new String(bytes,0,len3);
System.out.println(str3);//e
//3.釋放資源
fis.close();
}
}
2.6 字節(jié)流練習(xí)-文件拷貝
選擇一個(gè)比較小的文件,不要太大。大文件拷貝之后學(xué)。

(1)不使用字節(jié)數(shù)組拷貝
public class FileInputStreamDemo04 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//1.創(chuàng)建對(duì)象
FileInputStream fis = new FileInputStream("D:\\aaa\\movie.mp4");
FileOutputStream fos = new FileOutputStream("my-io\\copy.mp4");
//2.拷貝
//核心思想:邊讀邊寫
int b;
while((b = fis.read()) != -1){
fos.write(b);
}
//3.釋放資源
//規(guī)則:先開的最后關(guān)閉
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
(2)使用字節(jié)數(shù)組進(jìn)行拷貝
public class FileInputStreamDemo06 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//1.創(chuàng)建對(duì)象
FileInputStream fis = new FileInputStream("D:\\aaa\\movie.mp4");
FileOutputStream fos = new FileOutputStream("my-io\\copy.mp4");
//2.拷貝
int len;
byte[] bytes = new byte[1024 * 1024 * 5];
while((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
//3.釋放資源
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}流的關(guān)閉原則:先開后關(guān),后開先關(guān)。

3、字符流
當(dāng)使用字節(jié)流讀取文本文件時(shí),可能會(huì)有一個(gè)小問題。就是遇到中文字符時(shí),可能不會(huì)顯示完整的字符,那是因?yàn)橐粋€(gè)中文字符可能占用多個(gè)字節(jié)存儲(chǔ)。所以Java提供一些字符流類,以字符為單位讀寫數(shù)據(jù),專門用于處理文本文件。
3.1 字符輸入流 Reader
java.io.Reader抽象類是表示用于讀取字符流的所有類的超類,可以讀取字符信息到內(nèi)存中。它定義了字符輸入流的基本共性功能方法。
public void close():關(guān)閉此流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。public int read(): 從輸入流讀取一個(gè)字符。public int read(char[] cbuf): 從輸入流中讀取一些字符,并將它們存儲(chǔ)到字符數(shù)組 cbuf中 。
3.2 FileReader類
java.io.FileReader 類是讀取字符文件的便利類。構(gòu)造時(shí)使用系統(tǒng)默認(rèn)的字符編碼和默認(rèn)字節(jié)緩沖區(qū)。
- 字符編碼:字節(jié)與字符的對(duì)應(yīng)規(guī)則。Windows系統(tǒng)的中文編碼默認(rèn)是GBK編碼表;idea中是Unicode字符集、UTF-8編碼
- 字節(jié)緩沖區(qū):一個(gè)字節(jié)數(shù)組,用來臨時(shí)存儲(chǔ)字節(jié)數(shù)據(jù)。
3.2.1 構(gòu)造方法
創(chuàng)建一個(gè)流對(duì)象時(shí),必須傳入一個(gè)文件路徑,類似于FileInputStream 。
FileReader(File file): 創(chuàng)建一個(gè)新的 FileReader ,給定要讀取的File對(duì)象。FileReader(String fileName): 創(chuàng)建一個(gè)新的 FileReader ,給定要讀取的文件的名稱。
代碼示例:
public class FileReaderConstructor throws IOException{
public static void main(String[] args) {
// 使用File對(duì)象創(chuàng)建流對(duì)象
File file = new File("a.txt");
FileReader fr = new FileReader(file);
// 使用文件名稱創(chuàng)建流對(duì)象
FileReader fr = new FileReader("b.txt");
}
}3.2.2 讀取字符數(shù)據(jù)
第一步:創(chuàng)建對(duì)象 public FileReader(File file) 創(chuàng)建字符輸入流關(guān)聯(lián)本地文件 public FileReader(String pathname) 創(chuàng)建字符輸入流關(guān)聯(lián)本地文件 第二步:讀取數(shù)據(jù) public int read() 讀取數(shù)據(jù),讀到末尾返回-1 public int read(char[] buffer) 讀取多個(gè)數(shù)據(jù),讀到末尾返回-1 第三步:釋放資源 public void close() 釋放資源/關(guān)流
(1)讀取字符:read方法,每次可以讀取一個(gè)字符的數(shù)據(jù),提升為int類型,讀取到文件末尾,返回-1,循環(huán)讀取,代碼使用演示:

public class FileReaderDemo01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象并關(guān)聯(lián)本地文件
FileReader fr = new FileReader("my-io\\a.txt");
/**
* 2.讀取數(shù)據(jù) read()
* 字符流的底層也是字節(jié)流,默認(rèn)也是一個(gè)字節(jié)一個(gè)字節(jié)的讀取的。
* 如果遇到中文就會(huì)一次讀取多個(gè),GBK一次讀兩個(gè)字節(jié),UTF-8一次讀三個(gè)字節(jié)
* read()細(xì)節(jié):
* 1.read():默認(rèn)也是一個(gè)字節(jié)一個(gè)字節(jié)的讀取的,如果遇到中文就會(huì)一次讀取多個(gè)
* 2.在讀取之后,方法的底層還會(huì)進(jìn)行解碼并轉(zhuǎn)成十進(jìn)制。
* 最終把這個(gè)十進(jìn)制作為返回值,這個(gè)十進(jìn)制的數(shù)據(jù)也表示在字符集上的數(shù)字
* 英文:文件里面二進(jìn)制數(shù)據(jù) 0110 0001
* read方法進(jìn)行讀取,解碼并轉(zhuǎn)成十進(jìn)制97
* 中文:文件里面的二進(jìn)制數(shù)據(jù) 11100110 10110001 10001001
* read方法進(jìn)行讀取,解碼并轉(zhuǎn)成十進(jìn)制27721
* 想看到中文漢字,就是把這些十進(jìn)制數(shù)據(jù),再進(jìn)行強(qiáng)轉(zhuǎn)就可以了
*/
// 循環(huán)讀取
int ch;
while((ch = fr.read()) != -1){
System.out.println((char)ch);
}
//3.釋放資源
fr.close();
}
}雖然讀取了一個(gè)字符,但是會(huì)自動(dòng)提升為int類型。

(2)使用字符數(shù)組讀取:read(char[] cbuf),每次讀取b的長度個(gè)字符到數(shù)組中,返回讀取到的有效字符個(gè)數(shù),讀取到末尾時(shí),返回-1 ,代碼使用演示:

public class FileReaderDemo02 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
FileReader fr = new FileReader("my-io\\a.txt");
//2.讀取數(shù)據(jù)
char[] chars = new char[2];
int len; // 獲取有效的字符
//read(chars):讀取數(shù)據(jù),解碼,強(qiáng)轉(zhuǎn)三步合并了,把強(qiáng)轉(zhuǎn)之后的字符放到數(shù)組當(dāng)中
//空參的read + 強(qiáng)轉(zhuǎn)類型轉(zhuǎn)換
while((len = fr.read(chars)) != -1){
// 把數(shù)組中的數(shù)據(jù)變成字符串再進(jìn)行打印
System.out.println(new String(chars,0,len));
}
//3.釋放資源
fr.close();
}
}
3.3 字符輸出流 Writer
java.io.Writer 抽象類是表示用于寫出字符流的所有類的超類,將指定的字符信息寫出到目的地。它定義了字節(jié)輸出流的基本共性功能方法。
void write(int c)寫入單個(gè)字符。void write(char[] cbuf)寫入字符數(shù)組。abstract void write(char[] cbuf, int off, int len)寫入字符數(shù)組的某一部分,off數(shù)組的開始索引,len寫的字符個(gè)數(shù)。void write(String str)寫入字符串。void write(String str, int off, int len)寫入字符串的某一部分,off字符串的開始索引,len寫的字符個(gè)數(shù)。void flush()刷新該流的緩沖。void close()關(guān)閉此流,但要先刷新它。
3.4 FileWriter類
java.io.FileWriter 類是寫出字符到文件的便利類。構(gòu)造時(shí)使用系統(tǒng)默認(rèn)的字符編碼和默認(rèn)字節(jié)緩沖區(qū)。
3.4.1 構(gòu)造方法
創(chuàng)建一個(gè)流對(duì)象時(shí),必須傳入一個(gè)文件路徑,類似于FileOutputStream。
FileWriter(File file): 創(chuàng)建一個(gè)新的 FileWriter,給定要讀取的File對(duì)象。FileWriter(String fileName): 創(chuàng)建一個(gè)新的 FileWriter,給定要讀取的文件的名稱。
代碼示例:
public class FileWriterConstructor {
public static void main(String[] args) throws IOException {
// 使用File對(duì)象創(chuàng)建流對(duì)象
File file = new File("a.txt");
FileWriter fw = new FileWriter(file);
// 使用文件名稱創(chuàng)建流對(duì)象
FileWriter fw = new FileWriter("b.txt");
}
}3.4.2 基本寫出數(shù)據(jù)
寫出字符:write(int b) 方法,每次可以寫出一個(gè)字符數(shù)據(jù),代碼使用演示:
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileWriter fw = new FileWriter("fw.txt");
// 寫出數(shù)據(jù)
fw.write(97); // 寫出第1個(gè)字符
fw.write('b'); // 寫出第2個(gè)字符
fw.write('C'); // 寫出第3個(gè)字符
fw.write(30000); // 寫出第4個(gè)字符,中文編碼表中30000對(duì)應(yīng)一個(gè)漢字。
/*
【注意】關(guān)閉資源時(shí),與FileOutputStream不同。
如果不關(guān)閉,數(shù)據(jù)只是保存到緩沖區(qū),并未保存到文件。
*/
// fw.close();
}
}
輸出結(jié)果:
abC田
- 雖然參數(shù)為int類型四個(gè)字節(jié),但是只會(huì)保留一個(gè)字符的信息寫出。
- 未調(diào)用close方法,數(shù)據(jù)只是保存到了緩沖區(qū),并未寫出到文件中。
3.4.3 關(guān)閉和刷新
因?yàn)閮?nèi)置緩沖區(qū)的原因,如果不關(guān)閉輸出流,無法寫出字符到文件中。但是關(guān)閉的流對(duì)象,是無法繼續(xù)寫出數(shù)據(jù)的。如果既想寫出數(shù)據(jù),又想繼續(xù)使用流,就需要flush 方法了。
flush:刷新緩沖區(qū),流對(duì)象可以繼續(xù)使用。close:先刷新緩沖區(qū),然后通知系統(tǒng)釋放資源,流對(duì)象不可以再被使用了。
代碼使用演示:
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileWriter fw = new FileWriter("fw.txt");
// 寫出數(shù)據(jù),通過flush
fw.write('刷'); // 寫出第1個(gè)字符
fw.flush();
fw.write('新'); // 繼續(xù)寫出第2個(gè)字符,寫出成功
fw.flush();
// 寫出數(shù)據(jù),通過close
fw.write('關(guān)'); // 寫出第1個(gè)字符
fw.close();
fw.write('閉'); // 繼續(xù)寫出第2個(gè)字符,【報(bào)錯(cuò)】java.io.IOException: Stream closed
fw.close();
}
}即便是flush方法寫出了數(shù)據(jù),操作的最后還是要調(diào)用close方法,釋放系統(tǒng)資源。
3.4.4 寫出其他數(shù)據(jù)
第一步:創(chuàng)建對(duì)象
public FileWriter(File file) 創(chuàng)建字符輸出流關(guān)聯(lián)本地文件
public FileWriter(String pathname) 創(chuàng)建字符輸出流關(guān)聯(lián)本地文件
public FileWriter(File file, boolean append) 創(chuàng)建字符輸出流關(guān)聯(lián)本地文件,續(xù)寫
public FileWriter(String pathname, boolean append) 創(chuàng)建字符輸出流關(guān)聯(lián)本地文件,續(xù)寫
第二步:讀取數(shù)據(jù)
void write(int c) 寫出一個(gè)字符
void write(String str) 寫出一個(gè)字符串
void write(String str, int off, int len) 寫出一個(gè)字符串的一部分
void write(char[] cbuf) 寫出一個(gè)字符數(shù)組
void write(char[] cbuf, int off, int len) 寫出字符數(shù)組的一部分
第三步:釋放資源
public void close() 釋放資源/關(guān)流(1)寫出字符數(shù)組 :write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以寫出字符數(shù)組中的數(shù)據(jù),用法類似FileOutputStream,代碼使用演示:
public class FileWriterDemo01 {
public static void main(String[] args) throws IOException {
// 創(chuàng)建流對(duì)象并開啟續(xù)寫
FileWriter fw = new FileWriter("my-io\\a.txt",true);
//fw.write(25105);
//fw.write("你好威啊???");
char[] chars = {'a','b','c','我'};
fw.write(chars);
fw.close();
}
}
(2)寫出字符串:write(String str) 和 write(String str, int off, int len) ,每次可以寫出字符串中的數(shù)據(jù),更為方便,代碼使用演示:
public class FileWriterDemo03 {
public static void main(String[] args) throws IOException {
// 創(chuàng)建流對(duì)象,未開啟續(xù)寫
FileWriter fw = new FileWriter("my-io\\a.txt");
fw.write("我的同學(xué)各個(gè)都很厲害");
fw.write("說話聲音很好聽");
fw.flush();
fw.write("都是人才");
fw.write("超愛這里喲");
fw.close();
}
}
(3)續(xù)寫和換行:操作類似于FileOutputStream。
public class FileWriterDemo03 {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象,可以續(xù)寫數(shù)據(jù)
FileWriter fw = new FileWriter("my-io\\a.txt",true);
// 寫出字符串
fw.write("你好");
// 寫出換行
fw.write("\r\n");
// 寫出字符串
fw.write("我是張三");
// 關(guān)閉資源
fw.close();
}
}
4、IO異常的處理
4.1 JDK7前處理
之前的入門練習(xí),我們一直把異常拋出,而實(shí)際開發(fā)中并不能這樣處理,建議使用try...catch...finally 代碼塊,處理異常部分,代碼使用演示:
public class HandleException1 {
public static void main(String[] args) {
// 聲明變量
FileWriter fw = null;
try {
//創(chuàng)建流對(duì)象
fw = new FileWriter("fw.txt");
// 寫出數(shù)據(jù)
fw.write("我是張三");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}4.2 JDK7的處理
還可以使用JDK7優(yōu)化后的try-with-resource 語句,該語句確保了每個(gè)資源在語句結(jié)束時(shí)關(guān)閉。所謂的資源(resource)是指在程序完成后,必須關(guān)閉的對(duì)象。
格式:
try (創(chuàng)建流對(duì)象語句,如果多個(gè),使用';'隔開) {
// 讀寫數(shù)據(jù)
} catch (IOException e) {
e.printStackTrace();
}
代碼使用演示:
public class HandleException2 {
public static void main(String[] args) {
// 創(chuàng)建流對(duì)象
try ( FileWriter fw = new FileWriter("fw.txt"); ) {
// 寫出數(shù)據(jù)
fw.write("我是張三");
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.3 JDK9的改進(jìn)
JDK9中try-with-resource 的改進(jìn),對(duì)于引入對(duì)象的方式,支持的更加簡潔。被引入的對(duì)象,同樣可以自動(dòng)關(guān)閉,無需手動(dòng)close,我們來了解一下格式。
改進(jìn)前格式:
// 被final修飾的對(duì)象
final Resource resource1 = new Resource("resource1");
// 普通對(duì)象
Resource resource2 = new Resource("resource2");
// 引入方式:創(chuàng)建新的變量保存
try (Resource r1 = resource1;
Resource r2 = resource2) {
// 使用對(duì)象
}
改進(jìn)后格式:
// 被final修飾的對(duì)象
final Resource resource1 = new Resource("resource1");
// 普通對(duì)象
Resource resource2 = new Resource("resource2");
// 引入方式:直接引入
try (resource1; resource2) {
// 使用對(duì)象
}
改進(jìn)后,代碼使用演示:
public class TryDemo {
public static void main(String[] args) throws IOException {
// 創(chuàng)建流對(duì)象
final FileReader fr = new FileReader("in.txt");
FileWriter fw = new FileWriter("out.txt");
// 引入到try中
try (fr; fw) {
// 定義變量
int b;
// 讀取數(shù)據(jù)
while ((b = fr.read())!=-1) {
// 寫出數(shù)據(jù)
fw.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5、階段練習(xí)
5.1 拷貝文件夾
需求:拷貝一個(gè)文件夾,考慮子文件夾。原文件夾目錄結(jié)構(gòu)如下:
─src
│ aaa.txt
│
├─bbb
│ │ bbb.txt
│ │ bbb.xls
│ │
│ └─ccc
│ ccc.txt
│ ccc.xlsx
│
└─www
www.pptx代碼實(shí)現(xiàn):
public class Test01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象表示數(shù)據(jù)源
File src = new File("D:\\aaa\\src");
//2.創(chuàng)建對(duì)象表示目的地
File dest = new File("D:\\aaa\\dest");
//3.調(diào)用方法開始拷貝
copydir(src,dest);
}
/**
* 作用:拷貝文件夾
* @param src 數(shù)據(jù)源
* @param dest 目的地
*/
private static void copydir(File src, File dest) throws IOException {
dest.mkdirs();
//遞歸
//1.進(jìn)入數(shù)據(jù)源
File[] files = src.listFiles();
//2.遍歷數(shù)組
for (File file : files) {
if(file.isFile()){
//3.判斷文件,拷貝
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));
byte[] bytes = new byte[1024];
int len;
while((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}else {
//4.判斷文件夾,遞歸
copydir(file, new File(dest,file.getName()));
}
}
}
}
5.2 文件加密
需求:為了保證文件的安全性,就需要對(duì)原始文件進(jìn)行加密存儲(chǔ),再使用的時(shí)候再對(duì)其進(jìn)行解密處理。
加密原理:對(duì)原始文件中的每一個(gè)字節(jié)數(shù)據(jù)進(jìn)行更改,然后將更改以后的數(shù)據(jù)存儲(chǔ)到新的文件中。
解密原理:讀取加密之后的文件,按照加密的規(guī)則反向操作,變成原始文件。
使用異或解決,異或一次加密,再異或一次解密。
^ : 異或
兩邊相同:false
兩邊不同:truepublic class Test02 {
public static void main(String[] args) throws IOException {
//加密
encryptionAndReduction(
new File("D:\\aaa\\src\\pic.jpg"),
new File("D:\\aaa\\src\\secret.png")
);
//解密
encryptionAndReduction(
new File("D:\\aaa\\src\\secret.png"),
new File("D:\\aaa\\src\\pic1.jpg")
);
}
public static void encryptionAndReduction(File src, File dest) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
int b;
while ((b = fis.read()) != -1) {
fos.write(b ^ 2);
}
//4.釋放資源
fos.close();
fis.close();
}
}加密


解密


5.3 數(shù)字排序
文本文件中有以下的數(shù)據(jù):
2-1-9-4-7-8
將文件中的數(shù)據(jù)進(jìn)行排序,變成以下的數(shù)據(jù):
1-2-4-7-8-9
實(shí)現(xiàn)方式一:

public class Test03 {
public static void main(String[] args) throws IOException {
//1.讀取數(shù)據(jù)
FileReader fr = new FileReader("my-io\\a.txt");
StringBuilder sb = new StringBuilder();
int ch;
while((ch = fr.read()) != -1){
sb.append((char)ch);
}
fr.close();
System.out.println(sb);
//2.排序
String str = sb.toString();
String[] arrStr = str.split("-");//2-1-9-4-7-8
ArrayList<Integer> list = new ArrayList<>();
for (String s : arrStr) {
int i = Integer.parseInt(s);
list.add(i);
}
Collections.sort(list);
System.out.println(list);
//3.寫出
FileWriter fw = new FileWriter("my-io\\a.txt");
for (int i = 0; i < list.size(); i++) {
if(i == list.size() - 1){
fw.write(list.get(i) + "");
}else{
fw.write(list.get(i) + "-");
}
}
fw.close();
}
}

實(shí)現(xiàn)方式二:

public class Test04 {
public static void main(String[] args) throws IOException {
//1.讀取數(shù)據(jù)
FileReader fr = new FileReader("my-io\\a.txt");
StringBuilder sb = new StringBuilder();
int ch;
while((ch = fr.read()) != -1){
sb.append((char)ch);
}
fr.close();
System.out.println(sb);
//2.排序
Integer[] arr = Arrays.stream(sb.toString()
.split("-"))
.map(Integer::parseInt)
.sorted()
.toArray(Integer[]::new);
//3.寫出
FileWriter fw = new FileWriter("my-io\\a.txt");
String s = Arrays.toString(arr).replace(", ","-");
String result = s.substring(1, s.length() - 1);
fw.write(result);
fw.close();
}
}

高級(jí)流
1、緩沖流
上面是一些基本的流,作為IO流的入門,接下來要學(xué)習(xí)一些更強(qiáng)大的流。比如能夠高效讀寫的緩沖流,能夠轉(zhuǎn)換編碼的轉(zhuǎn)換流,能夠持久化存儲(chǔ)對(duì)象的序列化流等等。這些功能更為強(qiáng)大的流,都是在基本的流對(duì)象基礎(chǔ)之上創(chuàng)建而來的,相當(dāng)于是對(duì)基本流對(duì)象的一種增強(qiáng)。
1.1 概述
緩沖流,也叫高效流,是對(duì)4個(gè)基本的FileXxx 流的增強(qiáng),所以也是4個(gè)流,按照數(shù)據(jù)類型分類:
- 字節(jié)緩沖流:
BufferedInputStream,BufferedOutputStream - 字符緩沖流:
BufferedReader,BufferedWriter
緩沖流的基本原理,是在創(chuàng)建流對(duì)象時(shí),會(huì)創(chuàng)建一個(gè)內(nèi)置的默認(rèn)大小的緩沖區(qū)數(shù)組,通過緩沖區(qū)讀寫,減少系統(tǒng)IO次數(shù),從而提高讀寫的效率。
1.2 字節(jié)緩沖流
1.2.1 構(gòu)造方法
public BufferedInputStream(InputStream in):創(chuàng)建一個(gè) 新的緩沖輸入流。public BufferedOutputStream(OutputStream out): 創(chuàng)建一個(gè)新的緩沖輸出流。
構(gòu)造舉例,代碼如下:
// 創(chuàng)建字節(jié)緩沖輸入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt"));
// 創(chuàng)建字節(jié)緩沖輸出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));1.2.2 效率測(cè)試
查詢API,緩沖流讀寫方法與基本的流是一致的,我們通過復(fù)制大文件(375MB),測(cè)試它的效率。
(1)基本流,代碼如下:
public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 記錄開始時(shí)間
long start = System.currentTimeMillis();
// 創(chuàng)建流對(duì)象
try (
FileInputStream fis = new FileInputStream("jdk9.exe");
FileOutputStream fos = new FileOutputStream("copy.exe")
){
// 讀寫數(shù)據(jù)
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
// 記錄結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("普通流復(fù)制時(shí)間:"+(end - start)+" 毫秒");
}
}
十幾分鐘過去了...(2)緩沖流,代碼如下:
public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 記錄開始時(shí)間
long start = System.currentTimeMillis();
// 創(chuàng)建流對(duì)象
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe"));
){
// 讀寫數(shù)據(jù)
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
// 記錄結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("緩沖流復(fù)制時(shí)間:"+(end - start)+" 毫秒");
}
}
緩沖流復(fù)制時(shí)間:8016 毫秒如何更快呢?使用數(shù)組的方式,代碼如下:
public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 記錄開始時(shí)間
long start = System.currentTimeMillis();
// 創(chuàng)建流對(duì)象
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe"));
){
// 讀寫數(shù)據(jù)
int len;
byte[] bytes = new byte[8*1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0 , len);
}
} catch (IOException e) {
e.printStackTrace();
}
// 記錄結(jié)束時(shí)間
long end = System.currentTimeMillis();
System.out.println("緩沖流使用數(shù)組復(fù)制時(shí)間:"+(end - start)+" 毫秒");
}
}
緩沖流使用數(shù)組復(fù)制時(shí)間:666 毫秒1.3 字符緩沖流
1.3.1 構(gòu)造方法
public BufferedReader(Reader in):創(chuàng)建一個(gè) 新的緩沖輸入流。public BufferedWriter(Writer out): 創(chuàng)建一個(gè)新的緩沖輸出流。
代碼示例:
// 創(chuàng)建字符緩沖輸入流
BufferedReader br = new BufferedReader(new FileReader("br.txt"));
// 創(chuàng)建字符緩沖輸出流
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));1.3.2 特有方法
字符緩沖流的基本方法與普通字符流調(diào)用方式一致,不再闡述,我們來看它們具備的特有方法。
- BufferedReader:
public String readLine(): 讀一行文字。 - BufferedWriter:
public void newLine(): 寫一行行分隔符,由系統(tǒng)屬性定義符號(hào)。
(1)readLine方法在讀取的時(shí)候,一次讀一整行,遇到回車換行結(jié)束,但是不會(huì)把回車換行讀到內(nèi)存當(dāng)中,代碼示例:

public class BufferedStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建字符緩沖輸入流的對(duì)象
BufferedReader br = new BufferedReader(new FileReader("my-io\\a.txt"));
//2.讀取數(shù)據(jù)
/*String line1 = br.readLine();
System.out.println(line1);
String line2 = br.readLine();
System.out.println(line2);*/
String line;
while ((( line = br.readLine()) != null)){
System.out.println(line);
}
//3.釋放資源
br.close();
}
}
(2)newLine方法演示,代碼如下:
public class BufferedStreamDemo04 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建字符緩沖輸出流的對(duì)象
BufferedWriter bw = new BufferedWriter(new FileWriter("my-io/b.txt",true));
//2.寫出數(shù)據(jù)
bw.write("你好");
bw.newLine();
bw.write("我是張三");
bw.newLine();
//3.釋放資源
bw.close();
}
}
1.4 練習(xí)-文本排序
需求:將文本信息恢復(fù)順序

分析:
- 逐行讀取文本信息。
- 把讀取到的文本存儲(chǔ)到集合中
- 對(duì)集合中的文本進(jìn)行排序
- 遍歷集合,按順序,寫出文本信息。
實(shí)現(xiàn)方式一:
public class Test06Case01 {
public static void main(String[] args) throws IOException {
//1.讀取數(shù)據(jù)
BufferedReader br = new BufferedReader(new FileReader("my-io\\csb.txt"));
String line;
ArrayList<String> list = new ArrayList<>();
while((line = br.readLine()) != null){
list.add(line);
}
br.close();
//2.排序
//排序規(guī)則:按照每一行前面的序號(hào)進(jìn)行排列
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//獲取o1和o2的序號(hào)
int i1 = Integer.parseInt(o1.split("\\.")[0]);
int i2 = Integer.parseInt(o2.split("\\.")[0]);
return i1 - i2;
}
});
//3.寫出
BufferedWriter bw = new BufferedWriter(new FileWriter("my-io\\csb-result.txt"));
for (String str : list) {
bw.write(str);
bw.newLine();
}
bw.close();
}
}
實(shí)現(xiàn)方式二:
public class Test06Case02 {
public static void main(String[] args) throws IOException {
//1.讀取數(shù)據(jù)
BufferedReader br = new BufferedReader(new FileReader("my-io\\csb.txt"));
String line;
TreeMap<Integer,String> tm = new TreeMap<>();
while((line = br.readLine()) != null){
String[] arr = line.split("\\.");
//0:序號(hào) 1 :內(nèi)容
tm.put(Integer.parseInt(arr[0]),line);
}
br.close();
//2.寫出數(shù)據(jù)
BufferedWriter bw = new BufferedWriter(new FileWriter("my-io\\csb-result2.txt"));
Set<Map.Entry<Integer, String>> entries = tm.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
String value = entry.getValue();
bw.write(value);
bw.newLine();
}
bw.close();
}
}
1.5 練習(xí)-程序運(yùn)行次數(shù)限制
實(shí)現(xiàn)一個(gè)驗(yàn)證程序運(yùn)行次數(shù)的小程序,要求如下:
1.當(dāng)程序運(yùn)行超過3次時(shí)給出提示:本軟件只能免費(fèi)使用3次,歡迎您注冊(cè)會(huì)員后繼續(xù)使用~
2.程序運(yùn)行演示如下:
第一次運(yùn)行控制臺(tái)輸出: 歡迎使用本軟件,第1次使用免費(fèi)~
第二次運(yùn)行控制臺(tái)輸出: 歡迎使用本軟件,第2次使用免費(fèi)~
第三次運(yùn)行控制臺(tái)輸出: 歡迎使用本軟件,第3次使用免費(fèi)~
第四次及之后運(yùn)行控制臺(tái)輸出:本軟件只能免費(fèi)使用3次,歡迎您注冊(cè)會(huì)員后繼續(xù)使用~代碼實(shí)現(xiàn):
public class Test07 {
public static void main(String[] args) throws IOException {
//1.把文件中的數(shù)字讀取到內(nèi)存中
//創(chuàng)建IO流的原則:隨用隨創(chuàng)建,什么時(shí)候不用什么時(shí)候關(guān)閉
BufferedReader br = new BufferedReader(new FileReader("my-io\\count.txt"));
String line = br.readLine();
br.close(); // 讀完數(shù)據(jù)就關(guān)閉
int count = Integer.parseInt(line);
//表示當(dāng)前軟件又運(yùn)行了一次
count++;//1
//2.判斷
if(count <= 3 && count >= 0){
System.out.println("歡迎使用本軟件,第"+count+"次使用免費(fèi)~");
}else{
System.out.println("本軟件只能免費(fèi)使用3次,歡迎您注冊(cè)會(huì)員后繼續(xù)使用~");
}
BufferedWriter bw = new BufferedWriter(new FileWriter("my-io\\count.txt"));
//3.把當(dāng)前自增之后的count寫出到文件當(dāng)中
bw.write(count + ""); // 轉(zhuǎn)成字符串
bw.close();
}
}
第一次運(yùn)行


第二次運(yùn)行


第三次運(yùn)行


第四次運(yùn)行


2、轉(zhuǎn)換流
2.1 字符編碼和字符集
GBK:中文2個(gè)字節(jié),英文一個(gè)字節(jié)
UTF-8:中文3個(gè)字節(jié),英文一個(gè)字節(jié)(UTF-8不是字符集,Unicode才是字符集,UTF-8只是Unicode字符集下的一種編碼方式)
2.1.1 字符編碼
編碼:字符(能看懂的)–字節(jié)(看不懂的)
解碼:字節(jié)(看不懂的)–>字符(能看懂的)
字符編碼Character Encoding : 就是一套自然語言的字符與二進(jìn)制數(shù)之間的對(duì)應(yīng)規(guī)則。
編碼表:生活中文字和計(jì)算機(jī)中二進(jìn)制的對(duì)應(yīng)規(guī)則
2.1.2 字符集
- 字符集
Charset:也叫編碼表。是一個(gè)系統(tǒng)支持的所有字符的集合,包括各國家文字、標(biāo)點(diǎn)符號(hào)、圖形符號(hào)、數(shù)字等。
計(jì)算機(jī)要準(zhǔn)確的存儲(chǔ)和識(shí)別各種字符集符號(hào),需要進(jìn)行字符編碼,一套字符集必然至少有一套字符編碼。常見字符集有ASCII字符集、GBK字符集、Unicode字符集等。

ASCII字符集 :
- ASCII用于顯示現(xiàn)代英語,主要包括控制字符(回車鍵、退格、換行鍵等)和可顯示字符(英文大小寫字符、阿拉伯?dāng)?shù)字和西文符號(hào))。
- 基本的ASCII字符集,使用7位(bits)表示一個(gè)字符,共128字符。ASCII的擴(kuò)展字符集使用8位(bits)表示一個(gè)字符,共256字符,方便支持歐洲常用字符。
GBxxx字符集:
- GB2312:簡體中文碼表。一個(gè)小于127的字符的意義與原來相同,但兩個(gè)大于127的字符連在一起時(shí),就表示一個(gè)漢字,這樣大約可以組合了包含7000多個(gè)簡體漢字,此外數(shù)學(xué)符號(hào)、羅馬希臘的字母、日文的假名們都編進(jìn)去了,連在ASCII里本來就有的數(shù)字、標(biāo)點(diǎn)、字母都統(tǒng)統(tǒng)重新編了兩個(gè)字節(jié)長的編碼,這就是常說的"全角"字符,而原來在127號(hào)以下的那些就叫"半角"字符了。
- GBK:最常用的中文碼表。是在GB2312標(biāo)準(zhǔn)基礎(chǔ)上的擴(kuò)展規(guī)范,使用了雙字節(jié)編碼方案,共收錄了21003個(gè)漢字,完全兼容GB2312標(biāo)準(zhǔn),同時(shí)支持繁體漢字以及日韓漢字等。
- GB18030:最新的中文碼表。收錄漢字70244個(gè),采用多字節(jié)編碼,每個(gè)字可以由1個(gè)、2個(gè)或4個(gè)字節(jié)組成。支持中國國內(nèi)少數(shù)民族的文字,同時(shí)支持繁體漢字以及日韓漢字等。
Unicode字符集 :
它最多使用4個(gè)字節(jié)的數(shù)字來表達(dá)每個(gè)字母、符號(hào),或者文字。有三種編碼方案,UTF-8、UTF-16和UTF-32。最為常用的UTF-8編碼。
UTF-8編碼,可以用來表示Unicode標(biāo)準(zhǔn)中任何字符,它是電子郵件、網(wǎng)頁及其他存儲(chǔ)或傳送文字的應(yīng)用中,優(yōu)先采用的編碼。它使用一至四個(gè)字節(jié)為每個(gè)字符編碼,編碼規(guī)則:
- 128個(gè)US-ASCII字符,只需一個(gè)字節(jié)編碼。
- 拉丁文等字符,需要二個(gè)字節(jié)編碼。
- 大部分常用字(含中文),使用三個(gè)字節(jié)編碼。
- 其他極少使用的Unicode輔助字符,使用四字節(jié)編碼。
2.2 編碼引出的問題
在IDEA中,使用FileReader 讀取項(xiàng)目中的文本文件。由于IDEA的設(shè)置,都是默認(rèn)的UTF-8編碼,所以沒有任何問題。但是,當(dāng)讀取Windows系統(tǒng)中創(chuàng)建的文本文件時(shí),由于Windows系統(tǒng)的默認(rèn)是GBK編碼,就會(huì)出現(xiàn)亂碼。
public class ReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("E:\\File_GBK.txt");
int read;
while ((read = fileReader.read()) != -1) {
System.out.print((char)read);
}
fileReader.close();
}
}
輸出結(jié)果:
???那么如何讀取GBK編碼的文件呢?
2.3 InputStreamReader類
注意:這種做法是JDK11之前的,JDK11及之后使用FileReader指定字符集。
轉(zhuǎn)換流java.io.InputStreamReader,是Reader的子類,是從字節(jié)流到字符流的橋梁。它讀取字節(jié),并使用指定的字符集將其解碼為字符。它的字符集可以由名稱指定,也可以接受平臺(tái)的默認(rèn)字符集。
2.3.1 構(gòu)造方法
InputStreamReader(InputStream in): 創(chuàng)建一個(gè)使用默認(rèn)字符集的字符流。InputStreamReader(InputStream in, String charsetName): 創(chuàng)建一個(gè)指定字符集的字符流。
代碼示例:
InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt"));
InputStreamReader isr2 = new InputStreamReader(new FileInputStream("in.txt") , "GBK");
2.3.2 指定編碼讀取



JDK11之前的做法:使用InputStreamReader
public class ConvertStreamDemo02 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象并指定字符編碼(了解)
InputStreamReader isr = new InputStreamReader(new FileInputStream("my-io\\gbkfile.txt"),"GBK");
//2.讀取數(shù)據(jù)
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char)ch);
}
//3.釋放資源
isr.close();
}
}
JDK11及之后的做法:使用FileReader
public class ConvertStreamDemo02 {
public static void main(String[] args) throws IOException {
// JDK11之后的方法
FileReader fr = new FileReader("my-io\\gbkfile.txt", Charset.forName("GBK"));
//2.讀取數(shù)據(jù)
int ch2;
while ((ch2 = fr.read()) != -1){
System.out.print((char)ch2);
}
//3.釋放資源
fr.close();
}
}
2.4 OutputStreamWriter類
注意:這種做法是JDK11之前的,JDK11及之后使用FileWriter指定字符集。
轉(zhuǎn)換流java.io.OutputStreamWriter ,是Writer的子類,是從字符流到字節(jié)流的橋梁。使用指定的字符集將字符編碼為字節(jié)。它的字符集可以由名稱指定,也可以接受平臺(tái)的默認(rèn)字符集。
2.4.1 構(gòu)造方法
OutputStreamWriter(OutputStream in):創(chuàng)建一個(gè)使用默認(rèn)字符集的字符流。OutputStreamWriter(OutputStream in, String charsetName):創(chuàng)建一個(gè)指定字符集的字符流。
代碼示例:
OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream("out.txt"));
OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream("out.txt") , "GBK");
2.4.2 指定編碼寫出
JDK11之前:使用OutputStreamWriter
public class ConvertStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建轉(zhuǎn)換流的對(duì)象(JDK11之前)
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("my-io\\b.txt"),"GBK");
//2.寫出數(shù)據(jù)
osw.write("你好你好");
//3.釋放資源
osw.close();
}
}



JDK11及之后:使用FileWriter
public class ConvertStreamDemo03 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("my-io\\c.txt", Charset.forName("GBK"));
fw.write("你好你好");
fw.close();
}
}



2.5 練習(xí)-轉(zhuǎn)換文件編碼
將GBK編碼的文本文件,轉(zhuǎn)換為UTF-8編碼的文本文件。
- 指定GBK編碼的轉(zhuǎn)換流,讀取文本文件。
- 使用UTF-8編碼的轉(zhuǎn)換流,寫出文本文件。
public class ConvertStreamDemo04 {
public static void main(String[] args) throws IOException {
//方法一:JDK11以前的方案
InputStreamReader isr = new InputStreamReader(new FileInputStream("my-io\\gbkfile.txt"),"GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("my-io\\utf8file.txt"),"UTF-8");
int b;
while((b = isr.read()) != -1){
osw.write(b);
}
osw.close();
isr.close();
//方法二:JDK11及之后的方案
FileReader fr = new FileReader("my-io\\gbkfile.txt", Charset.forName("GBK"));
FileWriter fw = new FileWriter("my-io\\utf8file2.txt",Charset.forName("UTF-8"));
int b2;
while ((b2 = fr.read()) != -1){
fw.write(b2);
}
fw.close();
fr.close();
}
}
2.6 練習(xí)-按行讀取數(shù)據(jù)
利用字節(jié)流讀取文件中的數(shù)據(jù),每次讀一整行,而且不能出現(xiàn)亂碼,需要注意的是: 1.字節(jié)流在讀取中文的時(shí)候,是會(huì)出現(xiàn)亂碼的,但是字符流可以搞定 2.字節(jié)流里面是沒有讀一整行的方法的,只有字符緩沖流才能搞定


public class ConvertStreamDemo05 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("my-io\\a.txt")));
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}

3、序列化流
3.1 概述
Java 提供了一種對(duì)象序列化的機(jī)制。用一個(gè)字節(jié)序列可以表示一個(gè)對(duì)象,該字節(jié)序列包含該對(duì)象的數(shù)據(jù)、對(duì)象的類型和對(duì)象中存儲(chǔ)的屬性等信息。字節(jié)序列寫出到文件之后,相當(dāng)于文件中持久保存了一個(gè)對(duì)象的信息。
反之,該字節(jié)序列還可以從文件中讀取回來,重構(gòu)對(duì)象,對(duì)它進(jìn)行反序列化。對(duì)象的數(shù)據(jù)、對(duì)象的類型和對(duì)象中存儲(chǔ)的數(shù)據(jù)信息,都可以用來在內(nèi)存中創(chuàng)建對(duì)象。
3.2 ObjectOutputStream類
java.io.ObjectOutputStream 類,將Java對(duì)象的原始數(shù)據(jù)類型寫出到文件,實(shí)現(xiàn)對(duì)象的持久存儲(chǔ)。
3.2.1 構(gòu)造方法
public ObjectOutputStream(OutputStream out) : 創(chuàng)建一個(gè)指定OutputStream的ObjectOutputStream,即把基本流變成高級(jí)流。
FileOutputStream fileOut = new FileOutputStream("employee.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
3.2.2 序列化操作
(1)一個(gè)對(duì)象要想序列化,必須滿足兩個(gè)條件:
- 該類必須實(shí)現(xiàn)
java.io.Serializable接口,Serializable是一個(gè)標(biāo)記接口,不實(shí)現(xiàn)此接口的類將不會(huì)使任何狀態(tài)序列化或反序列化,會(huì)拋出NotSerializableException。 - 該類的所有屬性必須是可序列化的。如果有一個(gè)屬性不需要可序列化的,則該屬性必須注明是瞬態(tài)的,使用
transient關(guān)鍵字修飾。
public class Student implements Serializable {
private String name;
private int age;
//transient:瞬態(tài)關(guān)鍵字
//作用:不會(huì)把當(dāng)前屬性序列化到本地文件當(dāng)中
private transient String address;
public Student() {
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + ", address = " + address + "}";
}
}(2)寫出對(duì)象方法
public final void writeObject (Object obj):將指定的對(duì)象寫出。
public class ObjectStreamDemo01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建對(duì)象
Student stu = new Student("zhangsan",23,"北京");
//2.創(chuàng)建序列化流的對(duì)象/對(duì)象操作輸出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my-io\\a.txt"));
//3.寫出數(shù)據(jù)
oos.writeObject(stu);
//4.釋放資源
oos.close();
}
}
3.3 ObjectInputStream類
ObjectInputStream反序列化流,將之前使用ObjectOutputStream序列化的原始數(shù)據(jù)恢復(fù)為對(duì)象。
3.3.1 構(gòu)造方法
public ObjectInputStream(InputStream in) : 創(chuàng)建一個(gè)指定InputStream的ObjectInputStream,即把基本流變成高級(jí)流。
3.3.2 反序列化操作
如果能找到一個(gè)對(duì)象的class文件,可以進(jìn)行反序列化操作,調(diào)用ObjectInputStream讀取對(duì)象的方法:
public final Object readObject () : 讀取一個(gè)對(duì)象。
public class ObjectStreamDemo02 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.創(chuàng)建反序列化流的對(duì)象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("my-io\\a.txt"));
//2.讀取數(shù)據(jù)
Student o = (Student) ois.readObject();
//3.打印對(duì)象
System.out.println(o);
//4.釋放資源
ois.close();
}
}
對(duì)于JVM可以反序列化對(duì)象,它必須是能夠找到class文件的類。如果找不到該類的class文件,則拋出一個(gè) ClassNotFoundException 異常。
3.3.3 反序列化操作問題
當(dāng)JVM反序列化對(duì)象時(shí),能找到class文件,但是class文件在序列化對(duì)象之后,實(shí)體類進(jìn)行了修改(比如修改了屬性等),那么反序列化操作也會(huì)失敗,拋出一個(gè)InvalidClassException異常。發(fā)生這個(gè)異常的原因如下:
- 該類的序列版本號(hào)與從流中讀取的類描述符的版本號(hào)不匹配
- 該類包含未知數(shù)據(jù)類型
- 該類沒有可訪問的無參數(shù)構(gòu)造方法
解決:
Serializable接口給需要序列化的類,提供了一個(gè)序列版本號(hào)。serialVersionUID該版本號(hào)的目的在于驗(yàn)證序列化的對(duì)象和對(duì)應(yīng)類是否版本匹配。
idea可以自動(dòng)設(shè)置版本號(hào):



注意:注解@Serial在JDK14及以上才支持。
3.4 練習(xí)-序列化集合
需求:
- 將存有多個(gè)自定義對(duì)象的集合序列化操作,保存到
list.txt文件中。 - 反序列化
list.txt,并遍歷集合,打印對(duì)象信息。
分析:
- 把若干學(xué)生對(duì)象 ,保存到集合中。
- 把集合序列化。
- 反序列化讀取時(shí),只需要讀取一次,轉(zhuǎn)換為集合類型。
- 遍歷集合,可以打印所有的學(xué)生信息
代碼實(shí)現(xiàn):
public class Student implements Serializable {
@Serial
private static final long serialVersionUID = 8447688314497035445L;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + ", address = " + address + "}";
}
}(1)序列化
public class Test08 {
public static void main(String[] args) throws IOException {
//1.序列化多個(gè)對(duì)象
Student s1 = new Student("老王", 23,"北京");
Student s2 = new Student("老張", 22,"上海" );
Student s3 = new Student("老李",20,"南京");
ArrayList<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my-io\\a.txt"));
oos.writeObject(list);
oos.close();
}
}
(2)反序列化
public class Test09 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.創(chuàng)建反序列化流的對(duì)象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("my-io\\a.txt"));
//2.讀取數(shù)據(jù)
ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
for (Student student : list) {
System.out.println(student);
}
//3.釋放資源
ois.close();
}
}
(3)序列化和反序列化合并
public class Test10 {
public static void main(String[] args) throws Exception {
// 創(chuàng)建 學(xué)生對(duì)象
Student student = new Student("老王", 23,"北京");
Student student2 = new Student("老張", 22,"上海" );
Student student3 = new Student("老李",20,"南京");
ArrayList<Student> arrayList = new ArrayList<>();
arrayList.add(student);
arrayList.add(student2);
arrayList.add(student3);
// 序列化操作
serializ(arrayList);
// 反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("my-io/list.txt"));
// 讀取對(duì)象,強(qiáng)轉(zhuǎn)為ArrayList類型
ArrayList<Student> list = (ArrayList<Student>)ois.readObject();
for (Student s : list) {
System.out.println(s);
}
//3.釋放資源
ois.close();
}
private static void serializ(ArrayList<Student> arrayList) throws Exception {
// 創(chuàng)建 序列化流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my-io/list.txt"));
// 寫出對(duì)象
oos.writeObject(arrayList);
// 釋放資源
oos.close();
}
}

4、打印流
只有寫,沒有讀。
4.1 概述
平時(shí)我們?cè)诳刂婆_(tái)打印輸出,是調(diào)用print方法和println方法完成的,這兩個(gè)方法都來自于java.io.PrintStream類,該類能夠方便地打印各種數(shù)據(jù)類型的值,是一種便捷的輸出方式。
4.2 字節(jié)打印流PrintStream
4.2.1 構(gòu)造方法
public PrintStream(String fileName) : 使用指定的文件名創(chuàng)建一個(gè)新的打印流。
代碼示例:
PrintStream ps = new PrintStream("ps.txt");
4.2.2 改變打印流向
System.out就是PrintStream類型的,只不過它的流向是系統(tǒng)規(guī)定的,打印在控制臺(tái)上。不過,既然是流對(duì)象,我們就可以玩一個(gè)"小把戲",改變它的流向。
public class PrintDemo {
public static void main(String[] args) throws IOException {
// 調(diào)用系統(tǒng)的打印流,控制臺(tái)直接輸出97
System.out.println(97);
// 創(chuàng)建打印流,指定文件的名稱
PrintStream ps = new PrintStream("ps.txt");
// 設(shè)置系統(tǒng)的打印流流向,輸出到ps.txt
System.setOut(ps);
// 調(diào)用系統(tǒng)的打印流,ps.txt中輸出97
System.out.println(97);
}
}4.2.3 字節(jié)打印流基本使用
構(gòu)造方法:
public PrintStream(OutputStream/File/String) 關(guān)聯(lián)字節(jié)輸出流/文件/文件路徑 public PrintStream(String fileName, Charset charset) 指定字符編碼 public PrintStream(OutputStreamout, boolean autoFlush) 自動(dòng)刷新 public PrintStream(OutputStream out, boolean autoFlush, String encoding) 指定字符編碼且自動(dòng)刷新
成員方法:
public void write(int b) 常規(guī)方法:規(guī)則跟之前一樣,將指定的字節(jié)寫出 public void println(Xxx xx) 特有方法:打印任意數(shù)據(jù),自動(dòng)刷新,自動(dòng)換行 public void print(Xxx xx) 特有方法:打印任意數(shù)據(jù),不換行 public void printf(String format, Object... args) 特有方法:帶有占位符的打印語句,不換行
代碼示例:
public class PrintStreamDemo01 {
public static void main(String[] args) throws FileNotFoundException {
//1.創(chuàng)建字節(jié)打印流的對(duì)象
PrintStream ps = new PrintStream(new FileOutputStream("my-io\\a.txt"), true, Charset.forName("UTF-8"));
//2.寫出數(shù)據(jù)
ps.println(97); //寫出 + 自動(dòng)刷新 + 自動(dòng)換行
ps.print(true);
ps.println();
ps.printf("%s愛上了%s","阿珍","阿強(qiáng)");
//3.釋放資源
ps.close();
}
}

4.3 字符打印流
構(gòu)造方法:
public PrintWriter(Write/File/String) 關(guān)聯(lián)字節(jié)輸出流/文件/文件路徑 public PrintWriter(String fileName, Charset charset) 指定字符編碼 public PrintWriter(Write, boolean autoFlush) 自動(dòng)刷新 public PrintWriter(Write out, boolean autoFlush, String encoding) 指定字符編碼且自動(dòng)刷新
成員方法:
public void write(int b) 常規(guī)方法:規(guī)則跟之前一樣,將指定的字節(jié)寫出 public void println(Xxx xx) 特有方法:打印任意數(shù)據(jù),自動(dòng)刷新,自動(dòng)換行 public void print(Xxx xx) 特有方法:打印任意數(shù)據(jù),不換行 public void printf(String format, Object... args) 特有方法:帶有占位符的打印語句,不換行
代碼示例:
public class PrintStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建字符打印流的對(duì)象
PrintWriter pw = new PrintWriter(new FileWriter("my-io\\a.txt"),true);
//2.寫出數(shù)據(jù)
pw.println("今天你終于叫我名字了,雖然叫錯(cuò)了,但是沒關(guān)系,我馬上改");
pw.print("你好你好");
pw.printf("%s愛上了%s","阿珍","阿強(qiáng)");
//3.釋放資源
pw.close();
}
}
5、壓縮流和解壓縮流
Java中只能識(shí)別zip格式的。
壓縮流:負(fù)責(zé)壓縮文件或者文件夾
解壓縮流:負(fù)責(zé)把壓縮包中的文件和文件夾解壓出來
5.1 解壓流 ZipInputStream
可以解壓多級(jí)文件夾
public class ZipStreamDemo01 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建一個(gè)File表示要解壓的壓縮包
File src = new File("D:\\aaa\\src.zip");
//2.創(chuàng)建一個(gè)File表示解壓的目的地
File dest = new File("D:\\aaa\\dest");
//調(diào)用方法
unzip(src,dest);
}
//定義一個(gè)方法用來解壓
public static void unzip(File src,File dest) throws IOException {
//解壓的本質(zhì):把壓縮包里面的每一個(gè)文件或者文件夾讀取出來,按照層級(jí)拷貝到目的地當(dāng)中
//創(chuàng)建一個(gè)解壓縮流用來讀取壓縮包中的數(shù)據(jù)
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
//要先獲取到壓縮包里面的每一個(gè)zipentry對(duì)象
//表示當(dāng)前在壓縮包中獲取到的文件或者文件夾
ZipEntry entry;
while((entry = zip.getNextEntry()) != null){
System.out.println(entry);
if(entry.isDirectory()){
//文件夾:需要在目的地dest處創(chuàng)建一個(gè)同樣的文件夾
File file = new File(dest,entry.toString());
file.mkdirs();
}else{
//文件:需要讀取到壓縮包中的文件,并把他存放到目的地dest文件夾中(按照層級(jí)目錄進(jìn)行存放)
FileOutputStream fos = new FileOutputStream(new File(dest,entry.toString()));
int b;
while((b = zip.read()) != -1){
//寫到目的地
fos.write(b);
}
fos.close();
//表示在壓縮包中的一個(gè)文件處理完畢了。
zip.closeEntry();
}
}
zip.close();
}
}

5.2 壓縮流 ZipOutputStream
5.2.1 壓縮單個(gè)文件
public class ZipStreamDemo02 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建File對(duì)象表示要壓縮的文件
File src = new File("D:\\aaa\\a.txt");
//2.創(chuàng)建File對(duì)象表示壓縮包的位置
File dest = new File("D:\\aaa");
//3.調(diào)用方法用來壓縮
toZip(src,dest);
}
/**
* 壓縮
* @param src 表示要壓縮的文件
* @param dest 表示壓縮包的位置
*/
public static void toZip(File src,File dest) throws IOException {
//1.創(chuàng)建壓縮流關(guān)聯(lián)壓縮包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
//2.創(chuàng)建ZipEntry對(duì)象,表示壓縮包里面的每一個(gè)文件和文件夾
//參數(shù):壓縮包里面的路徑
ZipEntry entry = new ZipEntry("aaa\\bbb\\a.txt");
//3.把ZipEntry對(duì)象放到壓縮包當(dāng)中
zos.putNextEntry(entry);
//4.把src文件中的數(shù)據(jù)寫到壓縮包當(dāng)中
FileInputStream fis = new FileInputStream(src);
int b;
while((b = fis.read()) != -1){
zos.write(b);
}
zos.closeEntry();
zos.close();
}
}

5.2.2 壓縮多級(jí)文件夾
public class ZipStreamDemo03 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建File對(duì)象表示要壓縮的文件夾
File src = new File("D:\\aaa\\src");
//2.創(chuàng)建File對(duì)象表示壓縮包放在哪里(壓縮包的父級(jí)路徑)
File destParent = src.getParentFile();
//3.創(chuàng)建File對(duì)象表示壓縮包的路徑
File dest = new File(destParent,src.getName() + ".zip");
//4.創(chuàng)建壓縮流關(guān)聯(lián)壓縮包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
//5.獲取src里面的每一個(gè)文件,變成ZipEntry對(duì)象,放入到壓縮包當(dāng)中
toZip(src,zos,src.getName());
//6.釋放資源
zos.close();
}
/**
* 獲取src里面的每一個(gè)文件,變成ZipEntry對(duì)象,放入到壓縮包當(dāng)中
* @param src 數(shù)據(jù)源
* @param zos 壓縮流
* @param name 壓縮包內(nèi)部的路徑
*/
public static void toZip(File src, ZipOutputStream zos, String name) throws IOException {
//1.進(jìn)入src文件夾
File[] files = src.listFiles();
//2.遍歷數(shù)組
for (File file : files) {
if(file.isFile()){
//3.判斷-文件,變成ZipEntry對(duì)象,放入到壓縮包當(dāng)中
ZipEntry entry = new ZipEntry(name + "\\" + file.getName());
zos.putNextEntry(entry);
//讀取文件中的數(shù)據(jù),寫到壓縮包
FileInputStream fis = new FileInputStream(file);
int b;
while((b = fis.read()) != -1){
zos.write(b);
}
fis.close();
zos.closeEntry();
}else{
//4.判斷-文件夾,遞歸
toZip(file,zos,name + "\\" + file.getName());
}
}
}
}


6、工具包 Commons-io
官網(wǎng):https://commons.apache.org/
Commons是apache開源基金組織提供的工具包,里面有很多幫助我們提高開發(fā)效率的API,比如:
- StringUtils:字符串工具類
- NumberUtils:數(shù)字工具類
- ArrayUtils:數(shù)組工具類
- RandomUtils:隨機(jī)數(shù)工具類
- DateUtils:日期工具類
- StopWatch:秒表工具類
- ClassUtils:反射工具類
- SystemUtils:系統(tǒng)工具類
- MapUtils:集合工具類
- Beanutils:bean工具類
- Commons-io:io的工具類
其中Commons-io是apache開源基金組織提供的一組有關(guān)IO操作的開源工具包,用于提高IO流的開發(fā)效率。
官網(wǎng):https://commons.apache.org/proper/commons-io/
文檔:https://commons.apache.org/proper/commons-io/apidocs/index.html
使用方式:
- 新建lib文件夾
- 把第三方j(luò)ar包粘貼到文件夾中
- 右鍵點(diǎn)擊add as a library

Commons-io中的常用方法:
FileUtils類
static void copyFile(File srcFile, File destFile) 復(fù)制文件
static void copyDirectory(File srcDir, File destDir) 復(fù)制文件夾
static void copyDirectoryToDirectory(File srcDir, File destDir) 復(fù)制文件夾
static void deleteDirectory(File directory) 刪除文件夾
static void cleanDirectory(File directory) 清空文件夾
static String readFileToString(File file, Charset encoding) 讀取文件中的數(shù)據(jù)變成成字符串
static void write(File file, CharSequence data, String encoding) 寫出數(shù)據(jù)
IOUtils類
public static int copy(InputStream input, OutputStream output) 復(fù)制文件
public static int copyLarge(Reader input, Writer output) 復(fù)制大文件
public static String readLines(Reader input) 讀取數(shù)據(jù)
public static void write(String data, OutputStream output) 寫出數(shù)據(jù)
代碼示例:
public class CommonsIODemo {
public static void main(String[] args) throws IOException {
// 文件復(fù)制
File src = new File("my-io\\a.txt");
File dest = new File("my-io\\copy.txt");
FileUtils.copyFile(src,dest);
// 文件夾復(fù)制
File src2 = new File("D:\\aaa\\src");
File dest2 = new File("D:\\aaa\\bbb");
FileUtils.copyDirectoryToDirectory(src2,dest2);
// 清除文件夾D:\aaa\bbb的內(nèi)容
File src3 = new File("D:\\aaa\\bbb");
FileUtils.cleanDirectory(src3);
}
}


7、工具包 Hutool
官網(wǎng):https://hutool.cn/
API文檔:https://plus.hutool.cn/apidocs/
中文使用文檔:https://doc.hutool.cn/pages/index/
Commons是國人開發(fā)的開源工具包,里面有很多幫助我們提高開發(fā)效率的API,比如:
- DateUtil:日期時(shí)間工具類
- TimeInterval:計(jì)時(shí)器工具類
- StrUtil:字符串工具類
- HexUtil:16進(jìn)制工具類
- HashUtil:Hash算法類
- ObjectUtil:對(duì)象工具類
- ReflectUtil:反射工具類
- TypeUtil:泛型類型工具類
- PageUtil:分頁工具類
- NumberUtil:數(shù)字工具類
使用方式:
- 新建lib文件夾
- 把第三方j(luò)ar包粘貼到文件夾中
- 右鍵點(diǎn)擊add as a library
常用方法:

FileUtil類: file:根據(jù)參數(shù)創(chuàng)建一個(gè)file對(duì)象 touch:創(chuàng)建文件,如果父目錄不存在也自動(dòng)創(chuàng)建 writeLines:把集合中的數(shù)據(jù)寫出到文件中,覆蓋模式。 appendLines:把集合中的數(shù)據(jù)寫出到文件中,續(xù)寫模式。 readLines:指定字符編碼,把文件中的數(shù)據(jù),讀到集合中。 readUtf8Lines:按照UTF-8的形式,把文件中的數(shù)據(jù),讀到集合中 copy:拷貝文件或者文件夾
代碼示例:
public class HuToolDemo {
public static void main(String[] args) {
// 拼接路徑創(chuàng)建File對(duì)象
File file1 = FileUtil.file("D:\\", "aaa", "bbb", "a.txt");
System.out.println(file1);//D:\aaa\bbb\a.txt
// touch創(chuàng)建文件,如果父目錄不存在也自動(dòng)創(chuàng)建
File touch = FileUtil.touch(file1);
System.out.println(touch);
}
}

public class HuToolDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("aaa");
list.add("aaa");
// writeLines把集合中的數(shù)據(jù)寫出到文件中,覆蓋模式
File file2 = FileUtil.writeLines(list, "D:\\aaa\\a.txt", "UTF-8");
System.out.println(file2);
// appendLines把集合中的數(shù)據(jù)寫出到文件中,續(xù)寫模式
File file3 = FileUtil.appendLines(list, "D:\\aaa\\a.txt", "UTF-8");
System.out.println(file3);
// readLines指定字符編碼,把文件中的數(shù)據(jù),讀到集合中
List<String> list2 = FileUtil.readLines("D:\\aaa\\a.txt", "UTF-8");
System.out.println(list2);
}
}


8、配置文件操作IO流
創(chuàng)建一個(gè)空的配置文件 a.properties
8.1 向配置文件中存放數(shù)據(jù)
Properties集合+IO流:Properties跟IO流結(jié)合的操作,向a.properties中寫入集合Properties中的數(shù)據(jù)
public class Test11 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建集合
Properties prop = new Properties();
//2.添加數(shù)據(jù)
prop.put("aaa","bbb");
prop.put("bbb","ccc");
prop.put("ddd","eee");
prop.put("fff","iii");
//3.把集合中的數(shù)據(jù)以鍵值對(duì)的形式寫到本地文件當(dāng)中
// 方法一:
/*FileOutputStream fos = new FileOutputStream("my-io\\a.properties");
prop.store(fos,"test");
fos.close();*/
// 方法二:
BufferedWriter bw = new BufferedWriter(new FileWriter("my-io\\a.properties"));
Set<Map.Entry<Object, Object>> entries = prop.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
bw.write(key + "=" + value);
bw.newLine();
}
bw.close();
}
}方法一:

方法二:

8.2 讀取配置文件的數(shù)據(jù)
Properties集合+IO流:讀取配置文件a.properties中的數(shù)據(jù),存放到Properties集合中并打印出來
public class Test12 {
public static void main(String[] args) throws IOException {
//1.創(chuàng)建集合
Properties prop = new Properties();
//2.讀取本地Properties文件里面的數(shù)據(jù)
FileInputStream fis = new FileInputStream("my-io\\a.properties");
prop.load(fis);
fis.close();
//3.打印集合
System.out.println(prop);
}
}

到此這篇關(guān)于Java IO流總結(jié)的文章就介紹到這了,更多相關(guān)Java IO流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot URL帶有斜杠的轉(zhuǎn)義字符百分之2F導(dǎo)致的400錯(cuò)誤
這篇文章主要介紹了解決springboot URL帶有斜杠的轉(zhuǎn)義字符百分之2F導(dǎo)致的400錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
利用Spring Boot如何開發(fā)REST服務(wù)詳解
這篇文章主要給大家介紹了關(guān)于利用Spring Boot如何開發(fā)REST服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
SpringBoot打包前重新拉取maven依賴的方法實(shí)現(xiàn)
在使用 Maven 構(gòu)建 Spring Boot 項(xiàng)目時(shí),如果希望在每次打包時(shí)都強(qiáng)制拉取依賴,可以通過以下方法實(shí)現(xiàn),本文給大家介紹了四種實(shí)現(xiàn)方法,并通過代碼講解的非常詳細(xì),需要的朋友可以參考下2024-12-12
解決springboot+shiro+thymeleaf頁面級(jí)元素的權(quán)限控制問題
這篇文章主要介紹了解決springboot+shiro+thymeleaf頁面級(jí)元素的權(quán)限控制問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐
本文主要介紹了SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐,以手機(jī)驗(yàn)證碼登錄為例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
劍指Offer之Java算法習(xí)題精講二叉樹的構(gòu)造和遍歷
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
MyBatis saveBatch 性能調(diào)優(yōu)的實(shí)現(xiàn)
本文主要介紹了MyBatis saveBatch 性能調(diào)優(yōu)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建
這篇文章主要介紹了windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2016-07-07

