Reader和Writer抽象类
Reader是定义java的流式字符输入流模式的抽象类
Writer是定义流式字符输出的抽象类
该类的方法都返回void值并在错条件下抛出IOException异常
FileReader和FileWriter
FileReader表示可以读取文件内容的Reader类
FileWriter 表示可以写文件的Weiter类
默认有缓冲区,缓冲区默认大小为8k
public static void main(String[] args) throws IOException { FileReader fileReader=new FileReader(new File("D:\\zhangsan.txt")); FileWriter fWriter=new FileWriter(new File("D:\\e.txt")); char[]buff=new char[100]; int data=0;//实际读到的字符个数 while((data=fileReader.read(buff))!=-1) { fWriter.write(buff, 0, data); //fWriter.flush(); 强制关闭 } fileReader.close(); fWriter.close(); }
BufferedReader和BufferedWriter
BufferedReader通过缓冲输入提高性能
BufferedWriter通过缓冲输出提高性能
public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader(new File("D:\\\\zhangsan.txt")); BufferedReader bufferedReader = new BufferedReader(fileReader); FileWriter fileWriter = new FileWriter(new File("D:\\\\e.txt")); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); bufferedWriter.write(line); bufferedWriter.newLine();// 换行 //bufferedWriter.flush(); } bufferedReader.close(); bufferedWriter.close(); }