1、轉換流
OutputStreamWrite:字符流通向字節流的橋樑 (將字符串按照指定編碼格式,經過程序,輸出到文件)
IntputStreamRead:字節流通向字符流的橋樑 (將文件中的內容按照指定編碼格式,輸入到程序中)
2、緩衝流
一、字節緩衝流:
字節緩衝輸出流:BufferedOutputStream
構造方法:BufferedOutputStream(FileOutputStream fos)
經常使用方法:BufferedOutputStream對象.write(byte[] bytes);
字節緩衝輸入流:BufferedInputStream
構造方法:BufferedInputStream(FileIntputStream fis)
經常使用方法:BufferedInputStream對象.read();數組
字節緩衝流複製文件: 編碼
1 public static void main(String[] args) throws IOException { 2 File f = new File("D:\\demo0723\\jdk1.8.zip"); 3 4 // 1.用普通字節流一個字節爲單位複製jdk 5 long state1 = System.currentTimeMillis(); 6 // 明確數據源 7 FileInputStream fis1 = new FileInputStream(f); 8 // 明確目的地 9 FileOutputStream fos1 = new FileOutputStream("D:\\demo0723\\jdk\\jdk1.8(1).zip"); 10 int len1 = 0; 11 while ((len1 = fis1.read()) != -1) { 12 fos1.write(len1); 13 } 14 fis1.close(); 15 fos1.close(); 16 long end1 = System.currentTimeMillis(); 17 System.out.println("普通的一個字節一個字節的複製:" + (end1 - state1)); 18 19 // 2.用普通字節流一個字節數組爲單位複製jdk 20 // 明確數據源 21 long start2 = System.currentTimeMillis(); 22 FileInputStream fis2 = new FileInputStream(f); 23 // 明確目的地 24 FileOutputStream fos2 = new FileOutputStream("D:\\demo0723\\jdk\\jdk1.8(2).zip"); 25 byte[] bytes1 = new byte[1024]; 26 int len2 = 0; 27 while ((len2 = fis2.read(bytes1)) != -1) { 28 fos2.write(bytes1, 0, len2); 29 } 30 fis2.close(); 31 fos2.close(); 32 long end2 = System.currentTimeMillis(); 33 System.out.println("普通的一個數組爲單位的複製:" + (end2 - start2)); 34 35 // 3.用緩衝字節流一個字節爲單位複製jdk 36 // 明確數據源 37 long start3 = System.currentTimeMillis(); 38 FileInputStream fis3 = new FileInputStream(f); 39 BufferedInputStream bis1 = new BufferedInputStream(fis3); 40 // 明確目的地 41 FileOutputStream fos3 = new FileOutputStream("D:\\demo0723\\jdk\\jdk1.8(3).zip"); 42 BufferedOutputStream bos1 = new BufferedOutputStream(fos3); 43 int len3 = 0; 44 while ((len3 = bis1.read()) != -1) { 45 bos1.write(len3); 46 } 47 bis1.close(); 48 bos1.close(); 49 long end3 = System.currentTimeMillis(); 50 System.out.println("使用緩衝字節流一個字節一個字節的複製:" + (end3 - start3)); 51 52 // 4.用緩衝字節流一個字節數組爲單位複製jdk 53 // 明確數據源 54 long start4 = System.currentTimeMillis(); 55 FileInputStream fis4 = new FileInputStream(f); 56 BufferedInputStream bis2 = new BufferedInputStream(fis4); 57 // 明確目的地 58 FileOutputStream fos4 = new FileOutputStream("D:\\demo0723\\jdk\\jdk1.8(4).zip"); 59 BufferedOutputStream bos2 = new BufferedOutputStream(fos4); 60 int len4 = 0; 61 byte[] bytes2 = new byte[1024]; 62 while ((len4 = bis2.read(bytes2)) != -1) { 63 bos2.write(bytes2, 0, len4); 64 } 65 bis2.close(); 66 bos2.close(); 67 long end4 = System.currentTimeMillis(); 68 System.out.println("使用緩衝字節流一個字節數組一個字節數組的複製:" + (end4 - start4)); 69 }
二、字符緩衝流:
字符緩衝輸出流:BufferedWrite
構造方法:BufferedWrite(FileWrite fw);
經常使用方法:BufferedWrite對象.write();
BufferedWrite對象.newLine();//寫入新行
字符緩衝輸入流:BufferedRead
構造方法:BufferedReader(FileReader fr)
經常使用方法:BufferedRead對象.readLine();//讀取一行數據,以null做爲結束標記spa
使用字符緩衝流複製文件:
code
1 public static void main(String[] args) throws IOException { 2 File f = new File("D:\\demo0723\\demo.txt"); 3 4 // 一、使用 緩衝流 + line 複製 5 long start1 = System.currentTimeMillis(); 6 // 明確數據源 7 FileReader fr1 = new FileReader(f); 8 // 添加緩衝流 9 BufferedReader br1 = new BufferedReader(fr1); 10 // 明確目的地 11 FileWriter fw1 = new FileWriter("D:\\demo0723\\jdk\\demo(1).txt"); 12 BufferedWriter bw1 = new BufferedWriter(fw1); 13 String line = null; 14 while ((line = br1.readLine()) != null) { 15 bw1.write(line); 16 bw1.newLine(); 17 } 18 br1.close(); 19 bw1.close(); 20 long end1 = System.currentTimeMillis(); 21 System.out.println("緩衝流 + line:" + (end1 - start1)); 22 23 // 二、使用普通字符流 24 // 明確數據源 25 long start2 = System.currentTimeMillis(); 26 FileReader fr2 = new FileReader(f); 27 // 明確目的地 28 FileWriter fw2 = new FileWriter("D:\\demo0723\\jdk\\demo(2).txt"); 29 int len = 0; 30 while ((len = fr2.read()) != -1) { 31 fw2.write(len); 32 } 33 fr2.close(); 34 fw2.close(); 35 long end2 = System.currentTimeMillis(); 36 System.out.println("普通字符流:" + (end2 - start2)); 37 38 // 三、使用字符數組複製 39 // 明確數據源 40 long start3 = System.currentTimeMillis(); 41 FileReader fr3 = new FileReader(f); 42 // 明確目的地 43 FileWriter fw3 = new FileWriter("D:\\demo0723\\jdk\\demo(3).txt"); 44 int len2 = 0; 45 char[] ch = new char[1024]; 46 while ((len2 = fr3.read(ch)) != -1) { 47 fw3.write(ch, 0, len2); 48 } 49 fr3.close(); 50 fw3.close(); 51 long end3 = System.currentTimeMillis(); 52 System.out.println("字符數組複製:" + (end3 - start3)); 53 54 // 四、使用字符緩衝流一個一個複製 55 // 明確數據源 56 long start4 = System.currentTimeMillis(); 57 FileReader fr4 = new FileReader(f); 58 // 添加緩衝流 59 BufferedReader br4 = new BufferedReader(fr4); 60 // 明確目的地 61 FileWriter fw4 = new FileWriter("D:\\demo0723\\jdk\\demo(4).txt"); 62 // 添加緩衝流 63 BufferedWriter bw4 = new BufferedWriter(fw4); 64 int len4 = 0; 65 while ((len4 = br4.read()) != -1) { 66 bw4.write(len4); 67 } 68 br4.close(); 69 bw4.close(); 70 long end4 = System.currentTimeMillis(); 71 System.out.println("使用字符緩衝流一個一個複製:" + (end4 - start4)); 72 73 // 五、緩衝流+字符數組 74 long start5 = System.currentTimeMillis(); 75 // 明確數據源 76 FileReader fr5 = new FileReader(f); 77 // 添加緩衝流 78 BufferedReader br5 = new BufferedReader(fr5); 79 // 明確目的地 80 FileWriter fw5 = new FileWriter("D:\\demo0723\\jdk\\demo(5).txt"); 81 BufferedWriter bw5 = new BufferedWriter(fw5); 82 int len5 = 0; 83 char[] ch5 = new char[1024]; 84 while ((len5 = br5.read(ch5)) != -1) { 85 bw5.write(ch5,0,len5); 86 } 87 br5.close(); 88 bw5.close(); 89 long end5 = System.currentTimeMillis(); 90 System.out.println("緩衝流+字符數組:" + (end5 - start5)); 91 }
Day18對象