1.FileReader与FileWriter
1 package io.demo.chars; 2 3 import java.io.FileReader; 4 5 //使用FileReader读取文件,可读取英文也可以读取中文等各种文字 6 public class filedemo01 { 7 public static void main(String[] args) throws Exception { 8 FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello.txt"); 9 // int data = 0; 10 // while ((data = fileReader.read()) != -1) { 11 // System.out.print((char) data); 12 // } 13 System.out.println("---------------------"); 14 15 char[] chars = new char[1024]; 16 int count = 0; 17 while ((count = fileReader.read(chars)) != -1) { 18 System.out.print(new String(chars, 0, count)); 19 } 20 21 fileReader.close(); 22 } 23 }
1 package io.demo.chars; 2 3 import java.io.FileWriter; 4 5 //写入文件 6 public class filedemo02 { 7 public static void main(String[] args) throws Exception { 8 FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello2.txt"); 9 for (int i = 0; i < 10; i++) { 10 fileWriter.write("我2D天下无敌! "); 11 fileWriter.flush(); 12 } 13 fileWriter.close(); 14 } 15 }
1 package io.demo.chars; 2 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 6 //复制文件,只能复制文本文件 7 public class filedemo03 { 8 public static void main(String[] args) throws Exception { 9 FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello2.txt"); 10 FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello3.txt"); 11 int data = 0; 12 while ((data = fileReader.read()) != -1) { 13 fileWriter.write(data); 14 } 15 fileReader.close(); 16 fileWriter.close(); 17 } 18 }
2.BufferedReader与BufferedWriter
1 package io.demo.chars; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 6 //BufferedReader读取文件 7 public class buffereddemo01 { 8 public static void main(String[] args) throws Exception { 9 FileReader fileReader = new FileReader("/Users/yangasen/Downloads/hello2.txt"); 10 BufferedReader bufferedReader = new BufferedReader(fileReader); 11 String str = ""; 12 while ((str = bufferedReader.readLine()) != null) { 13 System.out.println(str); 14 } 15 bufferedReader.close(); 16 } 17 }
1 package io.demo.chars; 2 3 import java.io.BufferedWriter; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 //BufferedWriter 8 public class buffereddemo02 { 9 public static void main(String[] args) throws IOException { 10 FileWriter fileWriter = new FileWriter("/Users/yangasen/Downloads/hello4.txt"); 11 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 12 for (int i = 0; i < 10; i++) { 13 bufferedWriter.write("莫非朕不知兵?"); 14 bufferedWriter.newLine(); 15 } 16 bufferedWriter.close(); 17 18 } 19 }
3.PrintWriter
1 package io.demo.chars; 2 3 import java.io.PrintWriter; 4 5 public class printdemo { 6 public static void main(String[] args) throws Exception { 7 PrintWriter printWriter = new PrintWriter("/Users/yangasen/Downloads/print.txt"); 8 printWriter.println(97); 9 10 printWriter.println(true); 11 12 printWriter.println(3.14); 13 14 printWriter.println('x'); 15 16 printWriter.close(); 17 } 18 }
4.InputStreamReader与OutputStreamWriter
1 package io.demo.chars; 2 3 4 import java.io.FileInputStream; 5 import java.io.InputStreamReader; 6 7 public class streamdemo01 { 8 public static void main(String[] args) throws Exception { 9 FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/hello3.txt"); 10 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); 11 int data = 0; 12 while ((data = inputStreamReader.read()) != -1) { 13 System.out.print((char) data); 14 } 15 inputStreamReader.close(); 16 } 17 }
1 package io.demo.chars; 2 3 import java.io.FileOutputStream; 4 import java.io.OutputStreamWriter; 5 6 public class streamdemo02 { 7 public static void main(String[] args) throws Exception { 8 FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/hello5.txt"); 9 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8"); 10 for (int i = 0; i < 10; i++) { 11 outputStreamWriter.write("君不见董卓,丁原乎? "); 12 } 13 outputStreamWriter.close(); 14 } 15 }