I/O流 - 转换流和缓冲流
一、转换流
OutputStreamWrite:字符流通向字节流的桥梁 (将字符串按照指定编码格式,通过程序,输出到文件)
IntputStreamRead:字节流通向字符流的桥梁 (将文件中的内容按照指定编码格式,输入到程序中)
二、缓冲流
1、字节缓冲流:
字节缓冲输出流: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 }
2、字符缓冲流:
字符缓冲输出流:BufferedWrite
构造方法:BufferedWrite(FileWrite fw);
常用方法:BufferedWrite对象.write();
BufferedWrite对象.newLine();//写入新行
字符缓冲输入流:BufferedRead
构造方法:BufferedReader(FileReader fr)
常用方法:BufferedRead对象.readLine();//读取一行数据,以null作为结束标记
使用字符缓冲流复制文件:
1 public static void main(String[] args) throws IOException { 2 File f = new File("D:\\demo0723\\demo.txt"); 3 4 // 1、使用 缓冲流 + 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 // 2、使用普通字符流 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 // 3、使用字符数组复制 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 // 4、使用字符缓冲流一个一个复制 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 // 5、缓冲流+字符数组 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