标签:style blog color io os java ar 2014 div
1
public class wori { public static void main(String args[]) { FileWriter out = null; FileReader in = null; int b = 0; int n = 0; //要是放到try里,最后一句无法通过,try也是作用域 try { in = new FileReader("d:/1.txt"); out = new FileWriter("d:/2.txt"); while((b = in.read()) != -1) { System.out.println(b); n++; out.write(b); } out.close(); in.close(); } catch(FileNotFoundException e) { System.out.println("File not found"); System.exit(-1); } catch(IOException e) { System.out.println("File read error;"); System.exit(-1); } System.out.println("一共有" + n + "个数"); } }
2
BufferedReader BufferedWriter
public class wori { public static void main(String args[]) { try { BufferedWriter out = new BufferedWriter(new FileWriter("d:/2.txt")); // BufferedReader in = new BufferedReader(new FileReader("d:/1.txt")); // String s; while((s = in.readLine()) != null) //readLine返回null { out.write(s); out.newLine(); // } out.close(); in.close(); } catch(IOException e) { System.out.println("File read error"); } } }
3
缓冲流InputStreamReader和OutputStreamWriter,可以实现一个字符一个字符的读写
public class wori { public static void main(String args[]) { try { OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:/1.txt")); // out.write("worinima"); out.close(); // out = new OutputStreamWriter(new FileOutputStream("d:/2.txt", true)); //true代表追加,不加true,原来的内容会被清除 out.write("cao"); out.close(); } catch(IOException e) { System.out.println("File read error"); System.exit(-1); } } }
标签:style blog color io os java ar 2014 div
原文地址:http://www.cnblogs.com/melon2014/p/3985146.html