Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查文件中是否写入了内容?_Java_Bufferedreader - Fatal编程技术网

Java 如何检查文件中是否写入了内容?

Java 如何检查文件中是否写入了内容?,java,bufferedreader,Java,Bufferedreader,我有一个相当棘手的问题 有没有办法检查文件中是否写入了内容 这是一段由编写的代码,我需要检查“Hello world”是否已写入文件 这对于检查是否将一个大数字写入文本文件非常有用。 提前谢谢你 public class Program { public static void main(String[] args) { String text = "Hello world"; BufferedWriter output = null;

我有一个相当棘手的问题

有没有办法检查文件中是否写入了内容


这是一段由编写的代码,我需要检查“Hello world”是否已写入文件

这对于检查是否将一个大数字写入文本文件非常有用。 提前谢谢你

public class Program {
    public static void main(String[] args) {
        String text = "Hello world";
        BufferedWriter output = null;
        try {
            File file = new File("example.txt");
            output = new BufferedWriter(new FileWriter(file));
            output.write(text);
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally {
          if ( output != null ) {
            output.close();
          }
        }
    }
}

写完文件后再读
写完文件后再读?但我不明白,如果写入操作成功,内容就是您刚刚编写的内容。如果您想确保数据已写入操作系统,请刷新。如果您想确保它已完全投入长期存储,请同步。您签出了吗?
public boolean writeToTXT(String text, String path)
{
    BufferedWriter output = null;
    try {
        File file = new File(path);
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.flush();
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally {
          if ( output != null ) {
            output.close();
          }
        }
        
    try(BufferedReader br = new BufferedReader(new FileReader(path))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    return sb.toString().equals(text); }
}