先來看一下流的概念:
在程式中所有的資料都是以流的方式進行傳輸或儲存的,程式需要資料的時候要使用輸入流讀取資料,而當程式需要将一些資料儲存起來的時候,就要使用輸出流完成。
程式中的輸入輸出都是以流的形式儲存的,流中儲存的實際上全都是位元組檔案。
位元組流與字元流
在java.io包中操作檔案内容的主要有兩大類:位元組流、字元流,兩類都分為輸入和輸出操作。在位元組流中輸出資料主要是使用OutputStream完成,輸入使的是InputStream,在字元流中輸出主要是使用Writer類完成,輸入流主要使用Reader類完成。(這四個都是抽象類)
操作流程
在Java中IO操作也是有相應步驟的,以檔案操作為例,主要的操作流程如下:
- 使用File類打開一個檔案
- 通過位元組流或字元流的子類,指定輸出的位置
- 進行讀/寫操作
- 關閉輸入/輸出
IO操作屬于資源操作,一定要記得關閉
位元組流
位元組流主要是操作byte類型資料,以byte數組為準,主要操作類就是OutputStream、InputStream
位元組輸出流:OutputStream
OutputStream是整個IO包中位元組輸出流的最大父類,此類的定義如下:
public abstract class OutputStream extends Object implements Closeable,Flushable
從以上的定義可以發現,此類是一個抽象類,如果想要使用此類的話,則首先必須通過子類執行個體化對象,那麼如果現在要操作的是一個檔案,則可以使用:FileOutputStream類。通過向上轉型之後,可以為OutputStream執行個體化
Closeable表示可以關閉的操作,因為程式運作到最後肯定要關閉
Flushable:表示重新整理,清空記憶體中的資料
FileOutputStream類的構造方法如下:
public FileOutputStream(File file)throws FileNotFoundException
寫資料:

1 import java.io.File;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 public class Test11 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 OutputStream out=new FileOutputStream(f);//如果檔案不存在會自動建立
10 String str="Hello World";
11 byte[] b=str.getBytes();
12 out.write(b);//因為是位元組流,是以要轉化成位元組數組進行輸出
13 out.close();
14 }
15 }

也可以一個位元組一個位元組進行輸出,如下:

1 import java.io.File;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 public class Test11 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 OutputStream out=new FileOutputStream(f);//如果檔案不存在會自動建立
10 String str="Hello World";
11 byte[] b=str.getBytes();
12 for(int i=0;i<b.length;i++){
13 out.write(b[i]);
14 }
15 out.close();
16 }
17 }

以上輸出隻會進行覆寫,如果要追加的話,請看FileOutputStream類的另一個構造方法:
public FileOutputStream(File file,boolean append)throws FileNotFoundException
在構造方法中,如果将append的值設定為true,則表示在檔案的末尾追加内容。

1 import java.io.File;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 public class Test11 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 OutputStream out=new FileOutputStream(f,true);//追加内容
10 String str="\r\nHello World";
11 byte[] b=str.getBytes();
12 for(int i=0;i<b.length;i++){
13 out.write(b[i]);
14 }
15 out.close();
16 }
17 }

檔案中換行為:\r\n
位元組輸入流:InputStream
既然程式可以向檔案中寫入内容,則就可以通過InputStream從檔案中把内容讀取進來,首先來看InputStream類的定義:
public abstract class InputStream extends Object implements Closeable
與OutputStream類一樣,InputStream本身也是一個抽象類,必須依靠其子類,如果現在是從檔案中讀取,就用FileInputStream來實作。
觀察FileInputStream類的構造方法:
public FileInputStream(File file)throws FileNotFoundException
讀檔案:

1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 public class Test12 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 InputStream in=new FileInputStream(f);
10 byte[] b=new byte[1024];
11 int len=in.read(b);
12 in.close();
13 System.out.println(new String(b,0,len));
14 }
15 }

但以上方法是有問題的,用不用開辟這麼大的一個位元組數組,明顯是浪費嘛,我們可以根據檔案的大小來定義位元組數組的大小,File類中的方法:public long length()

1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 public class Test13 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 InputStream in=new FileInputStream(f);
10 byte[] b=new byte[(int) f.length()];
11 in.read(b);
12 in.close();
13 System.out.println(new String(b));
14 }
15 }

我們換種方式,一個位元組一個位元組讀入~

1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 public class Test14 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 InputStream in=new FileInputStream(f);
10 byte[] b=new byte[(int) f.length()];
11 for(int i=0;i<b.length;i++){
12 b[i]=(byte) in.read();
13 }
14 in.close();
15 System.out.println(new String(b));
16 }
17 }

但以上情況隻适合知道輸入檔案的大小,不知道的話用如下方法:

1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 public class Test15 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 InputStream in=new FileInputStream(f);
10 byte[] b=new byte[1024];
11 int temp=0;
12 int len=0;
13 while((temp=in.read())!=-1){//-1為檔案讀完的标志
14 b[len]=(byte) temp;
15 len++;
16 }
17 in.close();
18 System.out.println(new String(b,0,len));
19 }
20 }

字元流
在程式中一個字元等于兩個位元組,那麼java提供了Reader、Writer兩個專門操作字元流的類。
字元輸出流:Writer
Writer本身是一個字元流的輸出類,此類的定義如下:
public abstract class Writer extends Object implements Appendable,Closeable,Flushable
此類本身也是一個抽象類,如果要使用此類,則肯定要使用其子類,此時如果是向檔案中寫入内容,是以應該使用FileWriter的子類。
FileWriter類的構造方法定義如下:
public FileWriter(File file)throws IOException
字元流的操作比位元組流操作好在一點,就是可以直接輸出字元串了,不用再像之前那樣進行轉換操作了。
寫檔案:

1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.io.Writer;
5
6 public class Test16 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 Writer out=new FileWriter(f);
10 String str="Hello World";
11 out.write(str);
12 out.close();
13 }
14 }

在預設情況下再次輸出會覆寫,追加的方法也是在構造函數上加上追加标記

1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.io.Writer;
5
6 public class Test17 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 Writer out=new FileWriter(f,true);//追加
10 String str="\r\nHello World";
11 out.write(str);
12 out.close();
13 }
14 }

字元輸入流:Reader
Reader是使用字元的方式從檔案中取出資料,Reader類的定義如下:
public abstract class Reader extends Objects implements Readable,Closeable
Reader本身也是抽象類,如果現在要從檔案中讀取内容,則可以直接使用FileReader子類。
FileReader的構造方法定義如下:
public FileReader(File file)throws FileNotFoundException
以字元數組的形式讀取出資料:

1 import java.io.File;
2 import java.io.FileReader;
3 import java.io.IOException;
4 import java.io.Reader;
5
6 public class Test18 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 Reader input=new FileReader(f);
10 char[] c=new char[1024];
11 int len=input.read(c);
12 input.close();
13 System.out.println(new String(c,0,len));
14 }
15 }

也可以用循環方式,判斷是否讀到底:

1 import java.io.File;
2 import java.io.FileReader;
3 import java.io.IOException;
4 import java.io.Reader;
5
6 public class Test19 {
7 public static void main(String[] args) throws IOException {
8 File f = new File("d:" + File.separator+"test.txt");
9 Reader input=new FileReader(f);
10 char[] c=new char[1024];
11 int temp=0;
12 int len=0;
13 while((temp=input.read())!=-1){
14 c[len]=(char) temp;
15 len++;
16 }
17 input.close();
18 System.out.println(new String(c,0,len));
19 }
20 }

位元組流與字元流的差別
位元組流和字元流使用是非常相似的,那麼除了操作代碼的不同之外,還有哪些不同呢?
位元組流在操作的時候本身是不會用到緩沖區(記憶體)的,是與檔案本身直接操作的,而字元流在操作的時候是使用到緩沖區的
位元組流在操作檔案時,即使不關閉資源(close方法),檔案也能輸出,但是如果字元流不使用close方法的話,則不會輸出任何内容,說明字元流用的是緩沖區,并且可以使用flush方法強制進行重新整理緩沖區,這時才能在不close的情況下輸出内容
那開發中究竟用位元組流好還是用字元流好呢?
在所有的硬碟上儲存檔案或進行傳輸的時候都是以位元組的方法進行的,包括圖檔也是按位元組完成,而字元是隻有在記憶體中才會形成的,是以使用位元組的操作是最多的。
如果要java程式實作一個拷貝功能,應該選用位元組流進行操作(可能拷貝的是圖檔),并且采用邊讀邊寫的方式(節省記憶體)。