Writer 常用方法 ```java import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class Test { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { Writer writer = new FileWriter(file); // writer.write(20320); // writer.write(22909); // writer.write("你好"); // char[] chars = {'你','好','世','界'}; // writer.write(chars,2,2); writer.write("你好HelloWorld", 2, 2); writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` ### 处理流 FileReader 继承自 InputStreamReader,即输入转换流,通过这个类可以将字节流转为字符流,字节是基本单位,相当于基础管道,同理,FileWriter 继承自 OutputStreamWriter。 InputStreamReader 和 OutputStreamWriter 分别是 Reader 和 Writer 的子类。 ```java import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Test2 { public static void main(String[] args) { try { //基础的字节输入流 InputStream inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt"); //添加处理流 InputStreamReader inputStreamReader = new InputStreamReader(inputStream); char[] chars = new char[1024]; int length = inputStreamReader.read(chars); System.out.println(chars); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` ```java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; public class Test3 { public static void main(String[] args) { String str = "你好 世界 Hello World"; File file = new File("/Users/southwind/Desktop/test2.txt"); try { OutputStream outputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); Writer writer = outputStreamWriter; writer.write(str); writer.flush(); writer.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` ### 缓冲流 输入缓冲流 字节输入缓冲流:BufferedInputStream ```java import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class Test4 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); InputStream inputStream; try { inputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); // int temp = 0; // while((temp = bufferedInputStream.read())!=-1) { // System.out.println(temp); // } byte[] bytes = new byte[1024]; int length = bufferedInputStream.read(bytes,10,10); for(byte byt:bytes) { System.out.println(byt); } bufferedInputStream.close(); inputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` 字符输入缓冲流 BufferedReader ```java import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public class Test5 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { Reader reader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(reader); // String str = ""; // while((str = bufferedReader.readLine())!=null) { // System.out.println(str); // } // int temp = 0; // while((temp = bufferedReader.read())!=-1) { // System.out.println(temp); // } char[] chars = new char[1024]; int length = bufferedReader.read(chars,10,10); for(char cha:chars) { System.out.println(cha); } bufferedReader.close(); reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ``` 输出缓冲流 字节输出缓冲流 BufferedOutputStream ```java import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Test6 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test2.txt"); try { OutputStream outputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); String str = "20世纪90年代,硬件领域出现了单片式计算机系统,这种价格低廉的系统一出现就立即引起了自动控制领域人员的注意,因为使用它可以大幅度提升消费类电子产品(如电视机顶盒、面包烤箱、移动电话等)的智能化程度。"; byte[] bytes = str.getBytes(); bufferedOutputStream.write(bytes,10,10); bufferedOutputStream.flush(); bufferedOutputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ```