tags:

views:

597

answers:

3

I've done this before once, I'm trying to replicate what I did so far and this is what I've got:

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter("file.P", true));
        System.out.println("entered");
        if (!(newUserName.isEmpty()) || (newUserPass.isEmpty())){
            writer.newLine();
            writer.write("hellotest123");
            writer.close();
        }

It seems to find file.P, which is just a txt file, but it doesn't seem to append anything onto it? It enters the code and passes the IF statement fine, but nothing is appended to the text file? I'm slightly stuck!

A: 

1) Can you try calling the writer.flush() ? The code looks like it calls flush on close but would be better to confirm this.

2) Can you also print the full location of the file ? Perhaps it is appending in the tmp directory or the wrong location?

Calm Storm
That's not necessary. `writer.close()` will flush the stream.
jarnbjo
close() should flush the Writer.http://java.sun.com/javase/6/docs/api/java/io/BufferedWriter.html#close%28%29
Vinnie
still nothing, no error messages or anything. All the code after the If is run, but doesnt seem to do anything!
KP65
+3  A: 

Are you sure it is finding file.P and not just creating a new one elsewhere in the file system? Try an absolute path, just to make certain you and the program are looking at the same file.

Edit:

Based on the comment that the file is on the class path you should be using the following method of resolving it:

MyClass.class.getResource("file.P");

This will find file.P on the classpath in the same "package" or folder as MyClass.class

Kris
ahh ok you were right, that seemed to work calling it from the absolute file path. But, as the file.P is in the same package is there no way i can get it to just look at that? I'd rather not hard code the file path. Thanks
KP65
A: 

shouldn't your if statement have two apostrophes and "&&"? requiring userand pass instead of no user or pass?

if (!(newUserName.isEmpty()) && !(newUserPass.isEmpty())){
Shane