/* * 写数据到文件 */ public class Client1 { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); public static void main(String[] args){ FileWriter fw = null; try { fw = new FileWriter("E:\\test\\Demo02\\demo.txt"); fw.write("12314541251541aaaaa" + LINE_SEPARATOR + "sffdsfsdfew"); fw.write("xxxxxxxxxxx"); } catch (IOException e) { e.printStackTrace(); } finally { if(fw != null) { try { fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /* * 从文件读取数据 */ public class Client2 { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); char[] chs = new char[1024]; try { FileReader fr = new FileReader("demo.txt"); int num = 0; try { while((num = fr.read(chs)) != -1) sb.append(new String(chs,0,num)); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(sb.toString()); } }