1 package day02.com.offcn.test; 2 3 import java.io.*; 4 5 /** 6 * @author: CC 7 * @date: 2019/9/8 8 */ 9 public class IO { 10 //字节流转字符流转高效流 11 public static void main(String[] args) throws IOException { 12 test3(); 13 } 14 //打印流 最简单的输出文档 15 public static void test3() throws IOException { 16 PrintWriter printWriter = new PrintWriter(new FileWriter("D:/aa.txt"),true); 17 printWriter.write("大所发生的"); 18 printWriter.close(); 19 } 20 public static void test() throws IOException { 21 FileInputStream fileInputStream = new FileInputStream("D:/aa.txt"); 22 FileOutputStream fileOutputStream = new FileOutputStream("D:/bb.txt"); 23 24 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); 25 OutputStreamWriter outPutStreamWriter = new OutputStreamWriter(fileOutputStream); 26 27 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 28 BufferedWriter bufferedWriter = new BufferedWriter(outPutStreamWriter); 29 30 int len = 0; 31 while ((len = bufferedReader.read())!= -1){ 32 bufferedWriter.write(len); 33 } 34 35 bufferedWriter.flush(); 36 37 bufferedReader.close(); 38 bufferedWriter.close(); 39 } 40 //字节流转高效流 41 public static void test2() throws IOException { 42 FileInputStream fileInputStream = new FileInputStream("D:/aa.txt"); 43 FileOutputStream fileOutputStream = new FileOutputStream("D:/bb.txt"); 44 45 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); 46 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); 47 int len = 0; 48 while ((len = bufferedInputStream.read())!=-1){ 49 bufferedOutputStream.write(len); 50 } 51 bufferedOutputStream.flush(); 52 53 bufferedInputStream.close(); 54 bufferedOutputStream.close(); 55 } 56 //字符流转高效流 57 public static void test1() throws IOException { 58 FileReader fileReader = new FileReader("D:/aa.txt"); 59 FileWriter fileWriter = new FileWriter("D:/bb.txt"); 60 BufferedReader bufferedReader = new BufferedReader(fileReader); 61 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 62 63 String len =""; 64 while ((len = bufferedReader.readLine())!= null){ 65 bufferedWriter.write(len); 66 } 67 68 bufferedWriter.flush(); 69 70 bufferedReader.close(); 71 bufferedWriter.close(); 72 } 73 }