Java文件IO写入

Java文件IO写入,java,file,io,Java,File,Io,我似乎无法写入文件。我成功地创建了文件和目录,并且没有遇到任何异常,但是当我打开文件时,它没有写入任何行 是否可能需要以某种方式保存对文件的更改?或者有没有其他方式让我看不到这种变化,即使它被认为已经发生了 File stalkerFolder = new File("plugins/logs"); File logFile = new File("plugins/logs/log.txt"); FileWriter fw; FileReader fr; BufferedWriter write

我似乎无法写入文件。我成功地创建了文件和目录,并且没有遇到任何异常,但是当我打开文件时,它没有写入任何行

是否可能需要以某种方式保存对文件的更改?或者有没有其他方式让我看不到这种变化,即使它被认为已经发生了

File stalkerFolder = new File("plugins/logs");
File logFile = new File("plugins/logs/log.txt");
FileWriter fw;
FileReader fr;
BufferedWriter writer;
BufferedReader reader;
boolean error = false;
try{
    if(!folder.exists()){
        folder.mkdir();
    }
    logFile.createNewFile();
}catch(Exception e){}
try{
    fw = new FileWriter("plugins/logs/log.txt");
    fr = new FileReader("plugins/logs/log.txt");
    writer = new BufferedWriter(fw);
    reader = new BufferedReader(fr);
} catch(Exception e){
    System.err.println("ERROR: CANNOT READ OR WRITE TO LOG FILE");
    System.err.println("Plugin terminated.");
    error = true;
}

System.out.println("writing to log");
//Record to log
        try{
            writer.write("test log message");
        }catch(Exception e){
            System.err.println("could not write to log");
            e.printStackTrace();
        }

我没有打印出任何错误,我确实成功地完成了写入日志的操作。

如果这是所有源代码,则永远不会刷新和关闭缓冲写入程序。

如果这是所有源代码,则永远不会刷新和关闭缓冲写入程序。

您需要关闭写入程序。将最后几行更改为此

//Record to log
try{
    writer.write("test log message");
    writer.close();
}catch(Exception e){
    System.err.println("could not write to log");
    e.printStackTrace();
}

我所做的只是添加编写器。关闭。

您需要关闭编写器。将最后几行更改为此

//Record to log
try{
    writer.write("test log message");
    writer.close();
}catch(Exception e){
    System.err.println("could not write to log");
    e.printStackTrace();
}
我所做的只是添加writer.close。

在写入缓冲流后,需要调用writer.flush。完成后,请确保关闭编写器!尝试:

...
System.out.println("writing to log");
//Record to log
        try{
            writer.write("test log message");
            writer.flush();
            writer.close(); 
        }catch(Exception e){
            System.err.println("could not write to log");
            e.printStackTrace();
        }
编辑:根据sblundy的回答,在这种情况下,调用close就足以刷新和关闭流。通常,您需要使用flush正确写入底层文件编写器,并使用close关闭流。

在写入缓冲流后,需要调用writer.flush。完成后,请确保关闭编写器!尝试:

...
System.out.println("writing to log");
//Record to log
        try{
            writer.write("test log message");
            writer.flush();
            writer.close(); 
        }catch(Exception e){
            System.err.println("could not write to log");
            e.printStackTrace();
        }

编辑:根据sblundy的回答,在这种情况下,调用close就足以刷新和关闭流。通常,您需要使用flush正确写入底层文件编写器,并关闭以关闭流。

您需要关闭编写器以确保所有数据都已写入

try{
        writer.write("test log message");
        writer.flush(); // <--- optional
    }catch(Exception e){
        System.err.println("could not write to log");
        e.printStackTrace();
    }

// after you recorded everything, close your writer
finally {
try {
    writer.close();
} catch(IOException ioex) { }
}

您需要关闭写入程序以确保所有数据都已写入

try{
        writer.write("test log message");
        writer.flush(); // <--- optional
    }catch(Exception e){
        System.err.println("could not write to log");
        e.printStackTrace();
    }

// after you recorded everything, close your writer
finally {
try {
    writer.close();
} catch(IOException ioex) { }
}

尝试将\n符号添加到字符串的结尾

writer.write("test log message\n");

尝试将\n符号添加到字符串的结尾

writer.write("test log message\n");

关闭之前是否也应该有writer.flush。每次写入之前是否需要再次打开它?如果要在不关闭的情况下输出到文件,可以使用writer.flush,如果要写入更多数据,可以这样做,如果您需要查看其间的更改,请确保每次刷新,然后在应用程序结束时关闭写入程序。@Porthos3不,您只需要在完成后关闭,上次写入之后。关闭之前是否也应该有writer.flush。每次写入之前是否需要再次打开它?如果要在不关闭的情况下输出到文件,可以使用writer.flush,如果要写入更多数据,可以这样做,如果您需要查看其间的更改,请确保每次刷新,然后在应用程序结束时关闭写入程序。@Porthos3否,您只需要在完成最后一次写入后关闭写入程序。正如digitaljoel所说,BufferedWriter只会写入内部缓冲区。当您调用flush时,缓冲区会写入底层FileWriter。正如digitaljoel所说,BufferedWriter只写入内部缓冲区。当您调用flush时,缓冲区会写入底层FileWriter。为什么它应该是注释?它简明扼要地表明他必须刷新缓冲写入程序,这是问题的解决方案。@Alderath答案似乎是在我投否决票时编辑的;我将删除它。@dial为什么它应该是一个注释?它简明扼要地表明他必须刷新缓冲写入程序,这是问题的解决方案。@Alderath答案似乎是在我投否决票时编辑的;我把它取下来。