FileWriter 字符输出流:
方法:
写入:write 刷新:flush
public static void main(String[] args) throws IOException { FileWriter fWriter = new FileWriter("c:\1.txt"); fWriter.write(100); char[] c = {'a','b','c','d'}; fWriter.write(c); //截取下标为1、2的元素 fWriter.write(c, 1, 2); fWriter.write("你好世界"); fWriter.flush();//刷新 fWriter.close(); }
FileReader 读取文本:
//flush 和close区别 //flush用来刷新缓冲区的 只有字符流才需要刷新 public static void main(String[] args) throws Exception { FileReader fileReader = new FileReader("c:\1.txt"); //一个个字符读取 // int len=0; // while((len=fileReader.read())!=-1){ // System.out.print((char)len); // } char[] ch =new char[1024];// 一次读1024个字符 int len= 0; while((len=fileReader.read(ch))!=-1){ System.out.println(new String(ch,0,len)); } fileReader.close(); }