java中如何读写文件

 

//读文件

public static String read(String fileName) throws IOException {
  StringBuffer buffer = new StringBuffer();
  BufferedReader in = new BufferedReader(new FileReader(fileName));
  String s;
  while( (s = in.readLine()) != null){
   buffer.append(s);
   buffer.append("\n");
  }
  in.close();
  return buffer.toString();
 }

//写文件

public static void write(String fileName,String text) throws IOException{
  PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
  out.print(text);
  out.close();
 }

posted @ 2013-06-20 23:03  ㊣执着㊣  阅读(255)  评论(0编辑  收藏  举报