Java- FileWriter/BufferedWriter - appending to end of a text file?

18,521

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

Share:
18,521
Pedro Cañibano Zuñiga
Author by

Pedro Cañibano Zuñiga

Updated on June 11, 2022

Comments

  • Pedro Cañibano Zuñiga
    Pedro Cañibano Zuñiga almost 2 years

    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!