1.轉換流
輸入\輸出流體系中提供了兩個轉換流,這兩個轉換流用於實現將字節流轉換爲字符流。
1.1OutputStreamWriter
OutputStreamWriter 是字符流通向字節流的橋樑:可使用指定的字符編碼表,將要寫入流中的字符編碼成字節。它的作用的就是,將字符串按照指定的編碼表轉成字節,在使用字節流將這些字節寫出去。
public static void writeCN() throwsException {
//創建與文件關聯的字節輸出流對象
FileOutputStreamfos = newFileOutputStream("c:\\cn8.txt");
//創建可以把字符轉成字節的轉換流對象,並指定編碼
OutputStreamWriterosw = newOutputStreamWriter(fos,"utf-8");
//調用轉換流,把文字寫出去,其實是寫到轉換流的緩衝區中
osw.write("你好");//寫入緩衝區。
osw.close();
}
OutputStreamWriter流對象,它到底如何把字符轉成字節輸出的呢?
其實在OutputStreamWriter流中維護自己的緩衝區,當我們調用OutputStreamWriter對象的write方法時,會拿着字符到指定的碼錶中進行查詢,把查到的字符編碼值轉成字節數存放到OutputStreamWriter緩衝區中。然後再調用刷新功能,或者關閉流,或者緩衝區存滿後會把緩衝區中的字節數據使用字節流寫到指定的文件中。
1.2InputStreamReader
InputStreamReader是字節流通向字符流的橋樑:它使用指定的字符編碼表讀取字節並將其解碼爲字符。它使用的字符集可以由名稱指定或顯式給定,或者可以接受平臺默認的字符集。
public class InputStreamReaderDemo {
publicstaticvoidmain(String[] args) throws IOException {
//演示字節轉字符流的轉換流
readCN();
}
publicstaticvoidreadCN() throwsIOException{
//創建讀取文件的字節流對象
InputStreamin = newFileInputStream("c:\\cn8.txt");
//創建轉換流對象
//InputStreamReader isr = newInputStreamReader(in);這樣創建對象,會用本地默認碼錶讀取,將會發生錯誤解碼的錯誤
InputStreamReaderisr = new InputStreamReader(in,"utf-8");
//使用轉換流去讀字節流中的字節
intch = 0;
while((ch= isr.read())!=-1){
System.out.println((char)ch);
}
//關閉流
isr.close();
}
}
注意:在讀取指定的編碼的文件時,一定要指定編碼格式,否則就會發生解碼錯誤,而發生亂碼現象。
2.緩衝流
Java中提高了一套緩衝流,它的存在,可提高IO流的讀寫速度。根據流的分類分類字節緩衝流與字符緩衝流。
2.1字節緩衝輸出流BufferedOutputStream
通過字節緩衝流,進行文件的讀寫操作 寫數據到文件的操作
構造方法:
public BufferedOutputStream(OutputStream out)創建一個新的緩衝輸出流,以將數據寫入指定的底層輸出流。
publicclass BufferedOutputStreamDemo01 {
publicstaticvoid main(String[] args) throwsIOException {
//寫數據到文件的方法
write();
}
/*
* 寫數據到文件的方法
* 1,創建流
* 2,寫數據
* 3,關閉流
*/
privatestaticvoid write() throwsIOException {
//創建基本的字節輸出流
FileOutputStream fileOut = newFileOutputStream("abc.txt");
//使用高效的流,把基本的流進行封裝,實現速度的提升
BufferedOutputStream out = new BufferedOutputStream(fileOut);
//2,寫數據
out.write("hello".getBytes());
//3,關閉流
out.close();
}
}
2.2字節緩衝輸入流 BufferedInputStream
構造方法:
public BufferedInputStream(InputStream in)
/*
* 從文件中讀取數據
* 1,創建緩衝流對象
* 2,讀數據,打印
* 3,關閉
*/
privatestaticvoid read() throwsIOException {
//1,創建緩衝流對象
FileInputStream fileIn = newFileInputStream("abc.txt");
//把基本的流包裝成高效的流
BufferedInputStream in = newBufferedInputStream(fileIn);
//2,讀數據
int ch = -1;
while ( (ch = in.read()) != -1 ) {
//打印
System.out.print((char)ch);
}
//3,關閉
in.close();
}
2.3字符緩衝輸出流 BufferedWriter
將文本寫入字符輸出流,緩衝各個字符,從而提供單個字符、數組和字符串的高效寫入。
方法:
void newLine() 根據當前的系統,寫入一個換行符
/*
*BufferedWriter 字符緩衝輸出流
* 方法
* public void newLine()寫入一個行分隔符
*
* 需求: 通過緩衝輸出流寫入數據到文件
* 分析:
* 1,創建流對象
* 2,寫數據
* 3,關閉流
*
*/
publicclass BufferedWriterDemo {
publicstaticvoid main(String[] args) throwsIOException {
//創建流
//基本字符輸出流
FileWriter fileOut = newFileWriter("file.txt");
//把基本的流進行包裝
BufferedWriter out = newBufferedWriter(fileOut);
//2,寫數據
for (int i=0; i<5; i++) {
out.write("hello");
out.newLine();
}
//3,關閉流
out.close();
}
}
2.4字符緩衝輸入流 BufferedReader
從字符輸入流中讀取文本,緩衝各個字符,從而實現字符、數組和行的高效讀取。
方法
public String readLine()讀取一個文本行,包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
/*
*BufferedReader 字符緩衝輸入流
*
* 方法:
* String readLine()
* 需求:從文件中讀取數據,並顯示數據
*/
publicclass BufferedReaderDemo {
publicstaticvoid main(String[] args) throwsIOException {
//1,創建流
BufferedReader in = newBufferedReader(new FileReader("file.txt"));
//2,讀數據
//一次一個字符
//一次一個字符數組
//一次讀取文本中一行的字符串內容
String line = null;
while( (line = in.readLine()) != null ){
System.out.println(line);
}
//3,關閉流
in.close();
}
}