1. IO概述
1.1 什么是IO
生活中,你肯定經(jīng)歷過這樣的場景。當(dāng)你編輯一個(gè)文本文件,忘記了ctrl+s
,可能文件就白白編輯了。當(dāng)你電腦上插入一個(gè)U盤,可以把一個(gè)視頻,拷貝到你的電腦硬盤里。那么數(shù)據(jù)都是在哪些設(shè)備上的呢?鍵盤、內(nèi)存、硬盤、外接設(shè)備等等。
我們把這種數(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ù)的流。
1.3 IO的流向說明圖解
1.4 頂級(jí)父類們
輸入流 | 輸出流 | |
---|---|---|
字節(jié)流 | 字節(jié)輸入流 InputStream |
字節(jié)輸出流 OutputStream |
字符流 | 字符輸入流 Reader |
字符輸出流 Writer |
2. 字節(jié)流
2.1 一切皆為字節(jié)
一切文件數(shù)據(jù)(文本、圖片、視頻等)在存儲(chǔ)時(shí),都是以二進(jìn)制數(shù)字的形式保存,都一個(gè)一個(gè)的字節(jié),那么傳輸時(shí)一樣如此。所以,字節(jié)流可以傳輸任意文件數(shù)據(jù)。在操作流的時(shí)候,我們要時(shí)刻明確,無論使用什么樣的流對(duì)象,底層傳輸?shù)氖冀K為二進(jìn)制數(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é)輸出流。
小貼士:
close方法,當(dāng)完成流的操作時(shí),必須調(diào)用此方法,釋放系統(tǒng)資源。
2.3 FileOutputStream類
OutputStream
有很多子類,我們從最簡單的一個(gè)子類開始。
java.io.FileOutputStream
類是文件輸出流,用于將數(shù)據(jù)寫出到文件。
構(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ù)。
- 構(gòu)造舉例,代碼如下:
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");
}
}
寫出字節(jié)數(shù)據(jù)
-
寫出字節(jié):
write(int b)
方法,每次可以寫出一個(gè)字節(jié)數(shù)據(jù),代碼使用演示:
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 寫出數(shù)據(jù)
fos.write(97); // 寫出第1個(gè)字節(jié)
fos.write(98); // 寫出第2個(gè)字節(jié)
fos.write(99); // 寫出第3個(gè)字節(jié)
// 關(guān)閉資源
fos.close();
}
}
輸出結(jié)果:
abc
小貼士:
- 雖然參數(shù)為int類型四個(gè)字節(jié),但是只會(huì)保留一個(gè)字節(jié)的信息寫出。
- 流操作完畢后,必須釋放系統(tǒng)資源,調(diào)用close方法,千萬記得。
-
寫出字節(jié)數(shù)組:
write(byte[] b)
,每次可以寫出數(shù)組中的數(shù)據(jù),代碼使用演示:
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 字符串轉(zhuǎn)換為字節(jié)數(shù)組
byte[] b = "程序員".getBytes();
// 寫出字節(jié)數(shù)組數(shù)據(jù)
fos.write(b);
// 關(guān)閉資源
fos.close();
}
}
輸出結(jié)果:
程序員
-
寫出指定長度字節(jié)數(shù)組:
write(byte[] b, int off, int len)
,每次寫出從off索引開始,len個(gè)字節(jié),代碼使用演示:
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 字符串轉(zhuǎn)換為字節(jié)數(shù)組
byte[] b = "abcde".getBytes();
// 寫出從索引2開始,2個(gè)字節(jié)。索引2是c,兩個(gè)字節(jié),也就是cd。
fos.write(b,2,2);
// 關(guān)閉資源
fos.close();
}
}
輸出結(jié)果:
cd
數(shù)據(jù)追加續(xù)寫
經(jīng)過以上的演示,每次程序運(yùn)行,創(chuàng)建輸出流對(duì)象,都會(huì)清空目標(biāo)文件中的數(shù)據(jù)。如何保留目標(biāo)文件中數(shù)據(jù),還能繼續(xù)添加新數(shù)據(jù)呢?
-
public FileOutputStream(File file, boolean append)
: 創(chuàng)建文件輸出流以寫入由指定的 File對(duì)象表示的文件。 -
public FileOutputStream(String name, boolean append)
: 創(chuàng)建文件輸出流以指定的名稱寫入文件。
這兩個(gè)構(gòu)造方法,參數(shù)中都需要傳入一個(gè)boolean類型的值,true
表示追加數(shù)據(jù),false
表示清空原有數(shù)據(jù)。這樣創(chuàng)建的輸出流對(duì)象,就可以指定是否追加續(xù)寫了,代碼使用演示:
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("fos.txt",true);
// 字符串轉(zhuǎn)換為字節(jié)數(shù)組
byte[] b = "abcde".getBytes();
// 寫出從索引2開始,2個(gè)字節(jié)。索引2是c,兩個(gè)字節(jié),也就是cd。
fos.write(b);
// 關(guān)閉資源
fos.close();
}
}
文件操作前:cd
文件操作后:cdabcde
寫出換行
Windows系統(tǒng)里,換行符號(hào)是\r\n
。把
以指定是否追加續(xù)寫了,代碼使用演示:
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名稱創(chuàng)建流對(duì)象
FileOutputStream fos = new FileOutputStream("fos.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();
}
}
輸出結(jié)果:
a
b
c
d
e
- 回車符
\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é)。
構(gòu)造方法
-
FileInputStream(File file)
: 通過打開與實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream ,該文件由文件系統(tǒng)中的 File對(duì)象 file命名。 -
FileInputStream(String name)
: 通過打開與實(shí)際文件的連接來創(chuàng)建一個(gè) FileInputStream ,該文件由文件系統(tǒng)中的路徑名 name命名。
當(dāng)你創(chuàng)建一個(gè)流對(duì)象時(shí),必須傳入一個(gè)文件路徑。該路徑下,如果沒有該文件,會(huì)拋出FileNotFoundException
。
- 構(gòu)造舉例,代碼如下:
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");
}
}
讀取字節(jié)數(shù)據(jù)
-
讀取字節(jié):
read
方法,每次可以讀取一個(gè)字節(jié)的數(shù)據(jù),提升為int類型,讀取到文件末尾,返回-1
,代碼使用演示:
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名稱創(chuàng)建流對(duì)象
FileInputStream fis = new FileInputStream("read.txt");
// 讀取數(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);
// 關(guān)閉資源
fis.close();
}
}
輸出結(jié)果:
a
b
c
d
e
-1
循環(huán)改進(jìn)讀取方式,代碼使用演示:
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名稱創(chuàng)建流對(duì)象
FileInputStream fis = new FileInputStream("read.txt");
// 定義變量,保存數(shù)據(jù)
int b ;
// 循環(huán)讀取
while ((b = fis.read())!=-1) {
System.out.println((char)b);
}
// 關(guān)閉資源
fis.close();
}
}
輸出結(jié)果:
a
b
c
d
e
小貼士:
- 雖然讀取了一個(gè)字節(jié),但是會(huì)自動(dòng)提升為int類型。
- 流操作完畢后,必須釋放系統(tǒng)資源,調(diào)用close方法,千萬記得。
-
使用字節(jié)數(shù)組讀取:
read(byte[] b)
,每次讀取b的長度個(gè)字節(jié)到數(shù)組中,返回讀取到的有效字節(jié)個(gè)數(shù),讀取到末尾時(shí),返回-1
,代碼使用演示:
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名稱創(chuàng)建流對(duì)象.
FileInputStream fis = new FileInputStream("read.txt"); // 文件中為abcde
// 定義變量,作為有效個(gè)數(shù)
int len ;
// 定義字節(jié)數(shù)組,作為裝字節(jié)數(shù)據(jù)的容器
byte[] b = new byte[2];
// 循環(huán)讀取
while (( len= fis.read(b))!=-1) {
// 每次讀取后,把數(shù)組變成字符串打印
System.out.println(new String(b));
}
// 關(guān)閉資源
fis.close();
}
}
輸出結(jié)果:
ab
cd
ed
錯(cuò)誤數(shù)據(jù)d
,是由于最后一次讀取時(shí),只讀取一個(gè)字節(jié)e
,數(shù)組中,上次讀取的數(shù)據(jù)沒有被完全替換,所以要通過len
,獲取有效的字節(jié),代碼使用演示:
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名稱創(chuàng)建流對(duì)象.
FileInputStream fis = new FileInputStream("read.txt"); // 文件中為abcde
// 定義變量,作為有效個(gè)數(shù)
int len ;
// 定義字節(jié)數(shù)組,作為裝字節(jié)數(shù)據(jù)的容器
byte[] b = new byte[2];
// 循環(huán)讀取
while (( len= fis.read(b))!=-1) {
// 每次讀取后,把數(shù)組的有效字節(jié)部分,變成字符串打印
System.out.println(new String(b,0,len));// len 每次讀取的有效字節(jié)個(gè)數(shù)
}
// 關(guān)閉資源
fis.close();
}
}
輸出結(jié)果:
ab
cd
e
小貼士:
使用數(shù)組讀取,每次讀取多個(gè)字節(jié),減少了系統(tǒng)間的IO操作次數(shù),從而提高了讀寫的效率,建議開發(fā)中使用。
2.6 字節(jié)流練習(xí):圖片復(fù)制
復(fù)制原理圖解
案例實(shí)現(xiàn)
復(fù)制圖片文件,代碼使用演示:
public class Copy {
public static void main(String[] args) throws IOException {
// 1.創(chuàng)建流對(duì)象
// 1.1 指定數(shù)據(jù)源
FileInputStream fis = new FileInputStream("D:\\test.jpg");
// 1.2 指定目的地
FileOutputStream fos = new FileOutputStream("test_copy.jpg");
// 2.讀寫數(shù)據(jù)
// 2.1 定義數(shù)組
byte[] b = new byte[1024];
// 2.2 定義長度
int len;
// 2.3 循環(huán)讀取
while ((len = fis.read(b))!=-1) {
// 2.4 寫出數(shù)據(jù)
fos.write(b, 0 , len);
}
// 3.關(guān)閉資源
fos.close();
fis.close();
}
}
小貼士:
流的關(guān)閉原則:先開后關(guān),后開先關(guān)。文章來源:http://www.zghlxwxcb.cn/news/detail-714241.html
后記
????????美好的一天,到此結(jié)束,下次繼續(xù)努力!欲知后續(xù),請看下回分解,寫作不易,感謝大家的支持!! ??????文章來源地址http://www.zghlxwxcb.cn/news/detail-714241.html
到了這里,關(guān)于從零開始學(xué)習(xí) Java:簡單易懂的入門指南之IO字節(jié)流(三十)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!