Cannot add to file using printwriter in java

I have the following code to initialize a printwriter object -

/* This function is used to initialise the printwriter element so that it can begin the task of writing data into assignor.txt file...
     * 
     */
    public void startwriterassignor(String filename, boolean appendToFile) {

        //pw = null;

        try 
        {

            if (appendToFile== true) 
            {

                    //If the file already exists, start writing at the end of it.
                    pw = new PrintWriter(new FileWriter(filename, true));

            }
            else {

                    pw = new PrintWriter(new FileWriter(filename, false));
                    //  this is equal to:
                    //  pw = new PrintWriter(new FileWriter(filename, false));

            }
            //pw.flush();

        }
        catch (IOException e) {
                e.printStackTrace();
        }

}

      

First, I call the above function using the call below -

startwriterassignor("assignor.txt", false);

      

After writing some data to a file, I call the same function again using the call below -

startwriterassignor("assignor.txt", true);

      

After the second call "startiwriterassignor" is written to the file (added). However, no new data is added to the assignor.txt file, how to fix this error?

0
java printwriter filewriter


source to share


No one has answered this question yet

See similar questions:

3
How to write multiple times to a text file using PrintWriter

or similar:

6170
Is Java "pass-by-reference" or "pass-by-value"?
3799
How do I read / convert an InputStream to a string in Java?
3324
How to generate random integers in a specific range in Java?
3073
How to efficiently iterate over each entry in a Java map?
3044
Making a memory leak with Java
2956
What is the difference between public, secure, batch and private in Java?
2936
When to use LinkedList over ArrayList in Java?
2853
How can I convert String to int in Java?
1571
How to avoid Java code in JSP files?
756
How to check if a file exists in Java?



All Articles
Loading...
X
Show
Funny
Dev
Pics