unable to append to file using printwriter in java

假装没事ソ 提交于 2019-12-25 03:58:31

问题


I have the following code to initialise 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 invoke the above function using the call below--

startwriterassignor("assignor.txt", false);

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

startwriterassignor("assignor.txt", true);

After the second call to 'startiwriterassignor', more data is written (appended) into the file. However new data is not being appended to the file assignor.txt, how do I rectify this error?

来源:https://stackoverflow.com/questions/7497058/unable-to-append-to-file-using-printwriter-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!