目錄
一、字節流概述
字節流分爲字節輸出流(outputStream)、字節輸入流(inputStream)
字節流直接對二進制數據進行操作;字節流一般用來對媒體文件操作,比如圖片
二、字節輸出流(outputStream)
java中的outputStream是控制字節輸出的抽象類,繼承了Closeable, Flushable,AutoCloseable接口,因爲是抽象類,所以在使用時我們必須使用他的子類對象實例化。Closeable接口提供了close方法,該方法用於關閉流,Flushable提供了了flush方法,該方法用於清空緩存區,並把數據寫到指定位置。
outputstream控制流的輸出,所以他的主要方法是write
public abstract void write(int b) 輸出單個字節數據
public void write(byte[] b) 輸出字節數組的數據
public void write(byte[] b,int off,int len) 輸出字節數組指定位置的數據
我們現在通過outputStream的子類FileoutputStream進行文件的輸出
FileOutputStream(File file)
FileOutputStream(File file, boolean append)
上面展示了FileOutputStream的兩個構造方法,第二個構造方法的第二個參數表示是覆蓋還是添加數據,true爲添加,false爲覆蓋。
public static void main(String[] args) throws IOException {
//1.創建File對象
File file=new File("F:"+File.separator+"test.txt");
//2.通過子類進行父類對象的實例化
OutputStream stream=new FileOutputStream(file);
//3.寫文件
stream.write("xxx".getBytes());
//4.關閉流
stream.close();
}
三、字節輸入流(inputStream)
java中的inputStream是控制字節輸入的抽象類,繼承了Closeable,AutoCloseable接口,你會發現和outputStream相比,少了一個Flushable接口,原因是字節輸出的時候需要一個緩衝區存放即將寫入的數據,而讀取數據並不需要緩衝區。
因爲inputStream是個抽象類,因此我們還是使用其子類,這裏依舊通過FileInputStream來介紹
FileInputStream(File file)
int read() //讀取一個字節,如果爲空則返回-1
int read(byte[] b) //讀取所有字節,返回字節長度,如果爲空則返回-1
int read(byte[] b, int off, int len) //讀取指定字節,返回字節長度,如果爲空則返回-1
int available() //返回可以讀到的字節長度
public static void main(String[] args) throws IOException {
File file=new File("F:"+File.separator+"test.txt");
//1.通過子類進行父類對象的實例化
InputStream stream=new FileInputStream(file);
//2.設置讀取容器
byte[] data=new byte[1024];
//3.讀取數據
stream.read(data);
System.out.println(new String(data));
//4.關閉流
stream.close();
}
四、字符流概述
字節流在進行輸出的時候使用的都是字節數組的形式,但是在大部分情況下,使用字符串的方式能更加簡化輸出操作,因此在jdk1.1之後出現了字符流。
五、字符輸出流(Writer)
Writer是控制字符輸出的抽象類,它繼承了Closeable, Flushable, Appendable,AutoCloseable接口,相比outputStream,這裏多了一個Appendable接口,Appendable提供一個append方法用於追加輸入。
Appendable append(char c)
Appendable append(CharSequence csq) .
Appendable append(CharSequence csq, int start, int end)
void write(char[] cbuf)
abstract void write(char[] cbuf, int off, int len)
void write(int c)
void write(String str)
void write(String str, int off, int len)
這裏用到最多的是void write(String str) 方法,我們通過Writer的子類FileWriter進行實踐
public static void main(String[] args) throws IOException {
//1.創建File對象
File file=new File("F:"+File.separator+"test.txt");
//2.通過子類進行父類對象的實例化
Writer writer=new FileWriter(file);
//3.寫入
writer.write("yyy");
//4.追加
writer.append("xxx");
//5.關閉
writer.close();
}
六、字符輸入流(Reader)
Reader是實現字符數據的輸入流類,Reader繼承了Closeable, AutoCloseable, Readable接口,Reader和InputStream類似,主要通過read()方法讀取數據:
int read()
int read(char[] cbuf)
abstract int read(char[] cbuf, int off, int len)
int read(CharBuffer target)
public static void main(String[] args) throws IOException {
File file=new File("F:"+File.separator+"test.txt");
Reader reader=new FileReader(file);
char[] data=new char[1024];
int len=reader.read(data);
System.out.println(new String(data,0,len));
reader.close();
}
七、對比與總結
前面的四種流均繼承了AutoCloseable接口,因此我們還可以通過AutoCloseable來自動關閉類,關閉的方式就是使用try-catch方法。
public static void main(String[] args) throws IOException {
try{
File file=new File("F:"+File.separator+"test.txt");
Reader reader=new FileReader(file);
char[] data=new char[1024];
int len=reader.read(data);
System.out.println(new String(data,0,len));
}catch (Exception e){
}
}
關於何時使用哪種流進行操作的問題,字節流屬於操作二進制數據流,因此在網絡傳輸、圖片等數據處理時更加常用。字節流不方便處理中文,因此中文或字符串的處理使用字符流。