文件流2

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//读写包含中文的文件跟字符串
public class FileRead_Test {
 public static void main(String[] args) throws IOException
 {
  readCn();
  writeCN();
  readWrite();
 }
 
 public static void readCn() throws IOException
 {
  
  File file = new File("E:\\java4.3.2.1.7workspace\\c.txt");
  int len = 0;
  FileReader fr = new FileReader(file);
  while((len = fr.read())!=-1)
  {
   System.out.println(len);
   System.out.println((char)len);
  }
  fr.close();
 }
 
 public static void writeCN() throws IOException
 {
  FileWriter fw = new FileWriter("E:\\java4.3.2.1.7workspace\\d.txt");
  fw.write("来来啊!");
  fw.flush();
  fw.close();
 }
 
 public static void readWrite() throws IOException
 {
  FileReader fr = new FileReader("E:\\java4.3.2.1.7workspace\\d.txt");
  FileWriter fw = new FileWriter("E:\\java4.3.2.1.7workspace\\f.txt");
  
  char[] buf = new char[1024];
  
  int len= 0;
  while((len = fr.read(buf))!= -1)
  {
   fw.write(buf,0, len);
  }
  
  fr.close();
  fw.close();
 }
}

你可能感兴趣的:(javalearn)