Java读取文件和写入文件
其他
2020-04-01 11:26:18
阅读次数: 0
Java读取文件和写入文件 1.package com.manze.stu; 2.import java.io.*; 3.//Java教程:http://www.javaweb.cc 4.public class IOTest { 5. 6. public static void samples() throws IOException { 7. 8. //1. 这是从文件中逐行读入数据 9. 10. BufferedReader in = new BufferedReader(new FileReader("D:\\info.txt" )); 11. String s="",s2=""; 12. while((s=in.readLine())!=null) 13. s2+=s+"\n"; 14. in.close(); 15. //2. 这是从一个字符串中逐个读入字节 16. StringReader in1 = new StringReader(s2); 17. int c; 18. while ((c = in1.read()) != -1) 19. System .out.print((char)c); 20. 21. //3. 这是将读取的字符串写入另个文件 22. try { 23. BufferedReader in2 = new BufferedReader(new StringReader(s2)); 24. PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("D:\\IODemo.txt" ))); 25. int lineCount = 1; 26. while ((s = in2.readLine()) != null ) 27. out1.println(s); 28. out1.close(); 29. } catch (EOFException e) { 30. System .err.println("End of stream" ); 31. } 32. } 33. public static void main(String[] arg) 34. { 35. try { 36. IOTest.samples(); 37. } catch (IOException e) { 38. // TODO Auto-generated catch block 39. e.printStackTrace(); 40. } 41. } 42. 43. } |
|
发布了923 篇原创文章 ·
获赞 11 ·
访问量 3万+
转载自blog.csdn.net/xiaoyaGrace/article/details/105193373