java轉換流和緩衝流
轉換流
OutputStreamWriter類
OutputStreamWriter 是字元流通向位元組流的橋樑:可使用指定的字元編碼表,將要寫入流中的字元編碼成位元組。它的作用的就是,將字串按照指定的編碼表轉成位元組,在使用位元組流將這些位元組寫出去。
程式碼演示:
public static void writeCN() throws Exception { //建立與檔案關聯的位元組輸出流物件 FileOutputStream fos = new FileOutputStream("c:\\cn8.txt"); //建立可以把字元轉成位元組的轉換流物件,並指定編碼OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8"); //呼叫轉換流,把文字寫出去,其實是寫到轉換流的緩衝區中 osw.write("你好");//寫入緩衝區。 osw.close(); }
InputStreamReader類
InputStreamReader 是位元組流通向字元流的橋樑:它使用指定的字元編碼表讀取位元組並將其解碼為字元。它使用的字符集可以由名稱指定或顯式給定,或者可以接受平臺預設的字符集。
程式碼演示
public class InputStreamReaderDemo {public static void main(String[] args) throws IOException { //演示位元組轉字元流的轉換流 readCN(); } public static void readCN() throws IOException{ //建立讀取檔案的位元組流物件 InputStream in = new FileInputStream("c:\\cn8.txt"); //建立轉換流物件 //InputStreamReader isr = new InputStreamReader(in);這樣建立物件,會用本地預設碼錶讀取,將會發生錯誤解碼的錯誤InputStreamReader isr = new InputStreamReader(in,"utf-8"); //使用轉換流去讀位元組流中的位元組 int ch = 0; while((ch = isr.read())!=-1){ System.out.println((char)ch); } //關閉流 isr.close(); } }
轉換流和子類區別
1、操作的是檔案。2、使用預設編碼。
總結:
位元組--->字元 : 看不懂的--->看的懂的。 需要讀。輸入流。 InputStreamReader
字元--->位元組 : 看的懂的--->看不懂的。 需要寫。輸出流。 OutputStreamWriter
緩衝流
位元組緩衝流
寫入資料到流中,位元組緩衝輸出流 BufferedOutputStream
讀取流中的資料,位元組緩衝輸入流 BufferedInputStream
public class BufferedOutputStreamDemo01 { public static void main(String[] args) throws IOException { //寫資料到檔案的方法 write(); } /* * 寫資料到檔案的方法 * 1,建立流 * 2,寫資料 * 3,關閉流 */ private static void write() throws IOException { //建立基本的位元組輸出流 FileOutputStream fileOut = new FileOutputStream("abc.txt"); //使用高效的流,把基本的流進行封裝,實現速度的提升 BufferedOutputStream out = new BufferedOutputStream(fileOut); //2,寫資料 out.write("hello".getBytes()); //3,關閉流 out.close(); } }
/* * 從檔案中讀取資料 * 1,建立緩衝流物件 * 2,讀資料,列印 * 3,關閉 */ private static void read() throws IOException { //1,建立緩衝流物件 FileInputStream fileIn = new FileInputStream("abc.txt"); //把基本的流包裝成高效的流 BufferedInputStream in = new BufferedInputStream(fileIn); //2,讀資料 int ch = -1; while ( (ch = in.read()) != -1 ) { //列印 System.out.print((char)ch); } //3,關閉 in.close(); }
使用基本的流與高效的流完成複製檔案
/* * 需求:將d:\\test.avi檔案進行復制 * 採用4種方式複製 * 方式1: 採用基本的流,一次一個位元組的方式複製 共耗時 224613毫秒 * 方式2: 採用基本的流,一個多個位元組的方式賦值 共耗時 327毫秒 * 方式3: 採用高效的流,一次一個位元組的方式複製 共耗時 2047毫秒 * 方式4: 採用高效的流,一個多個位元組的方式賦值 共耗時 96毫秒 * * 資料來源: d:\\test.avi * 目的地1: d:\\copy1.avi * 目的地2: d:\\copy2.avi * 目的地3: d:\\copy3.avi * 目的地4: d:\\copy4.avi * * 實現的步驟: * 1,指定資料來源 * 2,指定目的地 * 3,讀資料 * 4,寫資料 * 5,關閉流 * */ public class CopyAVI { public static void main(String[] args) throws IOException { //開始計時 long start = System.currentTimeMillis(); //方式1: 採用基本的流,一次一個位元組的方式複製 //method1("d:\\test.avi", "d:\\copy1.avi"); //方式2: 採用基本的流,一個多個位元組的方式賦值 //method2("d:\\test.avi", "d:\\copy2.avi"); //方式3: 採用高效的流,一次一個位元組的方式複製 //method3("d:\\test.avi", "d:\\copy3.avi"); //方式4: 採用高效的流,一個多個位元組的方式賦值 method4("d:\\test.avi", "d:\\copy4.avi"); //結束計時 long end = System.currentTimeMillis(); //列印耗時多少毫秒 System.out.println("共耗時 " +(end - start)+ "毫秒"); } //方式4: 採用高效的流,一個多個位元組的方式賦值 private static void method4(String src, String dest) throws IOException { //1,指定資料來源 BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); //2,指定目的地 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); //3,讀資料 byte[] buffer = new byte[1024]; int len = -1; while ( (len = in.read(buffer)) != -1) { //4,寫資料 out.write(buffer, 0, le n); } //5,關閉流 in.close(); out.close(); } //方式3: 採用高效的流,一次一個位元組的方式複製 private static void method3(String src, String dest) throws IOException { //1,指定資料來源 BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); //2,指定目的地 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); //3,讀資料 int ch = -1; while ((ch=in.read()) != -1) { //4,寫資料 out.write(ch); } //5,關閉流 in.close(); out.close(); } //方式2: 採用基本的流,一個多個位元組的方式賦值 private static void method2(String src, String dest) throws IOException { //1,指定資料來源 FileInputStream in = new FileInputStream(src); //2,指定目的地 FileOutputStream out = new FileOutputStream(dest); //3,讀資料 byte[] buffer = new byte[1024]; int len = -1; while ( (len=in.read(buffer)) != -1) { //4,寫資料 out.write(buffer, 0, len); } //5,關閉流 in.close(); out.close(); } //方式1: 採用基本的流,一次一個位元組的方式複製 private static void method1(String src, String dest) throws IOException { //1,指定資料來源 FileInputStream in = new FileInputStream(src); //2,指定目的地 FileOutputStream out = new FileOutputStream(dest); //3,讀資料 int ch = -1; while (( ch=in.read()) != -1) { //4,寫資料 out.write(ch); } //5,關閉流 in.close(); out.close(); } }
字元緩衝流
字元緩衝輸入流 BufferedReader
字元緩衝輸出流 BufferedWriter
/* * BufferedWriter 字元緩衝輸出流 * 方法 * public void newLine()寫入一個行分隔符 * * 需求: 通過緩衝輸出流寫入資料到檔案 * 分析: * 1,建立流物件 * 2,寫資料 * 3,關閉流 * */ public class BufferedWriterDemo { public static void main(String[] args) throws IOException { //建立流 //基本字元輸出流 FileWriter fileOut = new FileWriter("file.txt"); //把基本的流進行包裝 BufferedWriter out = new BufferedWriter(fileOut); //2,寫資料 for (int i=0; i<5; i++) { out.write("hello"); out.newLine(); } //3,關閉流 out.close(); } }
/* * BufferedReader 字元緩衝輸入流 * * 方法: * String readLine() * 需求:從檔案中讀取資料,並顯示資料 */ public class BufferedReaderDemo { public static void main(String[] args) throws IOException { //1,建立流 BufferedReader in = new BufferedReader(new FileReader("file.txt")); //2,讀資料 //一次一個字元 //一次一個字元陣列 //一次讀取文字中一行的字串內容 String line = null; while( (line = in.readLine()) != null ){ System.out.println(line); } //3,關閉流 in.close(); } }
使用字元緩衝流完成文字檔案的複製
/* * 採用高效的字元緩衝流,完成文字檔案的賦值 * * 資料來源: file.txt * 目的地: copyFile.txt * * 分析: * 1,指定資料來源, 是資料來源中讀資料,採用輸入流 * 2,指定目的地,是把資料寫入目的地,採用輸出流 * 3,讀資料 * 4,寫資料 * 5,關閉流 */ public class CopyTextFile { public static void main(String[] args) throws IOException { //1,指定資料來源, 是資料來源中讀資料,採用輸入流 BufferedReader in = new BufferedReader(new FileReader("file.txt")); //2,指定目的地,是把資料寫入目的地,採用輸出流 BufferedWriter out = new BufferedWriter(new FileWriter("copyFile.txt")); //3,讀資料 String line = null; while ( (line = in.readLine()) != null ) { //4,寫資料 out.write(line); //寫入換行符號 out.newLine(); Out.flush(); } //5,關閉流 out.close(); in.close(); } }
流的操作規律
明確一:要操作的資料是資料來源還是資料目的。
源:InputStream Reader
目的:OutputStream Writer
先根據需求明確要讀,還是要寫。
明確二:要操作的資料是位元組還是文字呢?
源:
位元組:InputStream
文字:Reader
目的:
位元組:OutputStream
文字:Writer
已經明確到了具體的體系上。
明確三:明確資料所在的具體裝置。
源裝置:
硬碟:檔案 File開頭。
記憶體:陣列,字串。
鍵盤:System.in;
網路:Socket
目的裝置:
硬碟:檔案 File開頭。
記憶體:陣列,字串。
螢幕:System.out
網路:Socket
完全可以明確具體要使用哪個流物件。
明確四:是否需要額外功能呢?
額外功能:
轉換嗎?轉換流。InputStreamReader OutputStreamWriter
高效嗎?緩衝區物件。BufferedXXX
InputStream
FileInputStream
BufferedInputStream
OuputStream
FileOutputStream
BufferedOuputStream
Writer
OutputStreamWriter
FileWriter
BufferedWriter
Reader
InputStreamReader
FileReader
BufferedReader