Porthos3
Porthos3

Reputation: 401

Java File IO Write

I cannot seem to write to a file. I create the file and directory successfully, and I don't hit any exceptions, yet when I open the file, it doesn't have any lines written to it.

Is it possible that I need to somehow save changes to the file? Or is there some other way that I may not end up seeing the change, even though it has supposedly been made?

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();
        }

I do not get any errors printed out, and I DO reach "writing to log" successfully.

Upvotes: 1

Views: 2313

Answers (5)

Arkady
Arkady

Reputation: 1269

Try to add \n symbol to the end of the string.

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

Upvotes: 0

user425495
user425495

Reputation:

You need to call writer.flush() after writing to the buffered stream. Make sure to close the writer when you are done! Try:

...
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();
        }

Edit: As per sblundy's answer, in this case a call to close() is sufficient to flush and close the stream. In general you will want to make use of flush() to properly write to the underlying file writer, and close() to close the stream.

Upvotes: 0

Hugues
Hugues

Reputation: 365

You need to close the writer to be sure all the data are written

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) { }
}

Upvotes: 1

sblundy
sblundy

Reputation: 61414

You need to close the writer. Change the last few lines to this.

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

All I did was add the writer.close().

Upvotes: 6

digitaljoel
digitaljoel

Reputation: 26574

If that's all of your source you are never flushing and closing the buffered writer.

Upvotes: 1

Related Questions