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


source to share





All Articles