2

Every day I update a csv file using a FileWriter. When we step into a new month I have to delete the data from the previous month. My below code only updates a data in a csv file so, please help in deleting the previous month's data.

At least I need to know how to delete the data in csv file using FileWriter, so that I can manage to code for deleting previous month data.

        private static void eventsUpdate(HttpServletRequest request,
                                         HttpServletResponse response)  {
            String date=request.getParameter("date");   //getting from jsp page 
            String event=request.getParameter("event"); //getting from jsp page


            File file = new File( "D:///events/events.csv");  
            if ( !file.exists() )
                file.createNewFile();

            FileWriter fw = new FileWriter(file,true);
            BufferedWriter writer = new BufferedWriter( fw );
            writer.write(date);
            writer.write(",");
            writer.write(event);

            System.out.println("writing into excel");
            writer.newLine();
            writer.close();
            fw.close();

        }
5
  • Why not simply delete the file each month? Commented Mar 8, 2014 at 6:29
  • Please define "delete previous month data" operation. Is it stored within events.csv? Commented Mar 8, 2014 at 6:54
  • yes i am storing the data in events.csv Commented Mar 8, 2014 at 7:01
  • Atleast i need to know how to delete the data in csv file using FileWriter, so that i can manage to code for deleting previous month data.. please help Commented Mar 8, 2014 at 7:05
  • Do you always want to delete the data, or should it be archived? Maybe prior to the !file.exists() logic you should check if the month has changed and the file should be renamed. Commented Mar 8, 2014 at 8:27

2 Answers 2

7

Actually when you use the true argument in your FileWriter instantiation, you create a file writer object in append mode.

FileWriter fw = new FileWriter(file,false);

If you don't use the true option, the file will be overwritten by the new content. Therefore, I would suggest to follow this roadmap:

  1. Read the file content as a whole, in a String
  2. Remove the content you want to remove from the specific String
  3. Overwrite the file content using your new content in the specific String

Hope I helped!

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest you that you may create unique name file like march2014, january2013 instead of deleting it. Every month you need to read your target file which is named by your month strategy.

For ex:

When you reach april2014, create a new file april2014 etc. and read it for that month.

2 Comments

While the advice is sound, the proposed naming scheme is horrible. Something like 2014-02 is much nicer to work with.
Yes. But I didn't tell you that you have to implement april2013,march2014 naming strategy. I told that you can create a new file montly instead of deleting the previous file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.