该程序既不读取也不写入文件

发布于 2024-12-12 06:34:57 字数 589 浏览 0 评论 0原文

代码

import java.io.*;
class tester {
 public static void main(String args[]) {
  try {
   FileReader reader = new FileReader(new File("d:\\UnderTest\\check123.txt"));
   FileWriter writer = new FileWriter(new File("d:\\UnderTest\\check123.txt"));
   BufferedReader br = new BufferedReader(reader);
   String s;
     while( (s=br.readLine()) != null ) {
       System.out.println(s);
     }
   writer.write("Shadow Shadow");
  } catch(Exception exc) {
     System.out.println(exc);
    }
  }
}

当我运行它时,该代码不写入任何内容,也不读取任何内容。这个程序的bug在哪里?

CODE

import java.io.*;
class tester {
 public static void main(String args[]) {
  try {
   FileReader reader = new FileReader(new File("d:\\UnderTest\\check123.txt"));
   FileWriter writer = new FileWriter(new File("d:\\UnderTest\\check123.txt"));
   BufferedReader br = new BufferedReader(reader);
   String s;
     while( (s=br.readLine()) != null ) {
       System.out.println(s);
     }
   writer.write("Shadow Shadow");
  } catch(Exception exc) {
     System.out.println(exc);
    }
  }
}

This code writes nothing and reads nothing when i run it. Where is the bug in this program ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-12-19 06:34:57

您确定当您第一次阅读时文本文件中存在内容吗?
您需要在 try-catch 块的 finally 块(当前代码中缺少)中关闭 Reader 和 Writer。关闭流会自动刷新内容。

Are you sure that when you read for first time then content is there in the text file ?
You need to close Reader and Writer in finally block (missing currently in your code) of your try-catch block. closing the stream flushes out content automatically.

风追烟花雨 2024-12-19 06:34:57

确保关闭读者和作者。使用写入器后,您将需要刷新内容或关闭写入器(执行相同的操作)。我测试了这个并且它有效。

import java.io.*;
class tester {
 public static void main(String args[]) {
  try {
  FileReader reader = new FileReader(new File("c:\\check123.txt"));
  FileWriter writer = new FileWriter(new File("c:\\check123.txt"));
  BufferedReader br = new BufferedReader(reader);
  writer.write("Shadow Shadow");     
  writer.close();

  String s;
    while( (s=br.readLine()) != null ) {
      System.out.println(s);
    }
  reader.close();

 } catch(Exception exc) {
    System.out.println(exc);
   }
}

}

Make sure you close the reader and the writer. After using the writer you will need to flush the contents or close the writer (which does the same thing). I tested this and it works.

import java.io.*;
class tester {
 public static void main(String args[]) {
  try {
  FileReader reader = new FileReader(new File("c:\\check123.txt"));
  FileWriter writer = new FileWriter(new File("c:\\check123.txt"));
  BufferedReader br = new BufferedReader(reader);
  writer.write("Shadow Shadow");     
  writer.close();

  String s;
    while( (s=br.readLine()) != null ) {
      System.out.println(s);
    }
  reader.close();

 } catch(Exception exc) {
    System.out.println(exc);
   }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文