JAVA_FileReader_FileWriter
1 import java.io.*; 2 public class TestFileReader { 3 public static void main(String[] args) { 4 FileReader fr = null; 5 int c = 0; 6 try { 7 fr = new FileReader("d:\\share\\java\\io\\TestFileReader.java"); 8 int ln = 0; 9 while ((c = fr.read()) != -1) { 10 //char ch = (char) fr.read(); 11 System.out.print((char)c); 12 //if (++ln >= 100) { System.out.println(); ln = 0;} 13 } 14 fr.close(); 15 } catch (FileNotFoundException e) { 16 System.out.println("找不到指定文件"); 17 } catch (IOException e) { 18 System.out.println("文件读取错误"); 19 } 20 21 } 22 }
1 import java.io.*; 2 public class TestFileWriter { 3 public static void main(String[] args) { 4 FileWriter fw = null; 5 try { 6 fw = new FileWriter("d:\\bak\\unicode.dat"); 7 for(int c=0;c<=50000;c++){ 8 fw.write(c); 9 } 10 fw.close(); 11 } catch (IOException e1) { 12 e1.printStackTrace(); 13 System.out.println("文件写入错误"); 14 System.exit(-1); 15 } 16 } 17 }
1 import java.io.*; 2 3 public class TestFileWriter2 { 4 public static void main(String[] args) throws Exception { 5 FileReader fr = new FileReader("d:/java/io/TestFileWriter2.java"); 6 FileWriter fw = new FileWriter("d:/java/io/TestFileWriter2.bak"); 7 int b; 8 while((b = fr.read()) != -1) { 9 fw.write(b); 10 } 11 fr.close(); 12 fw.close(); 13 } 14 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。