Java 实例 - 读取文件内容


以下实例演示了使用 readLine() 方法来读取文件 test.log 内容,其中 test.log 文件内容为:

  1. 菜鸟教程
  2. www.shouce.ren

java 代码如下:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. import java.io.*;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. try {
  11. BufferedReader in = new BufferedReader
  12. (new FileReader("test.log"));
  13. String str;
  14. while ((str = in.readLine()) != null) {
  15. System.out.println(str);
  16. }
  17. System.out.println(str);
  18. } catch (IOException e) {
  19. }
  20. }
  21. }

以上代码运行输出结果为:

  1. 菜鸟教程
  2. www.shouce.ren
  3. null