-1

I'm using Java FileWriter to take some data from one file, that will do undergo some transformation and be put in to an output file. I want the output file to be at least cleared, if not deleted after each time I run my app so my question is just how would I achieve this?

This is what I've got so far:

try {
    file = new File(filepath);
    fileWriter = new FileWriter(file, true);
    fileWriter.write(data.toJSONString() + "\n");
    fileWriter.flush();
    fileWriter.close();
} 
catch (IOException e) {
e.printStackTrace();
}
System.out.print(data + "\n");
System.out.print(o + "\n");
}
file.createNewFile();
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3259647
  • 95
  • 1
  • 12
  • Just `File#delete()` the file? – Josh M Feb 02 '14 at 00:58
  • [http://docs.oracle.com/javase/tutorial/essential/io/delete.html](http://docs.oracle.com/javase/tutorial/essential/io/delete.html) – AntonH Feb 02 '14 at 01:01
  • There's not a delete method on the file (I'm using FileWriter, not File, perhaps I need some further refactoring), and as for re-opening, there isn't a method simply called open, is it simply calling a new file writer on the file name to achieve this? – user3259647 Feb 02 '14 at 01:02
  • There's no delete method? Are you sure? Have you checked the API for the File object? You still need a File to use a FileWriter -- so delete the File. – Hovercraft Full Of Eels Feb 02 '14 at 01:07
  • `File myFile = new File(path);` then `FileWriter fileWriter = new FileWriter(myFile);`,... use your FileWriter object, then `file.delete()`. You can't say that there's no delete method for File unless you look in the API, something you should do regularly. – Hovercraft Full Of Eels Feb 02 '14 at 01:11
  • I can wrap File in FileWriter and delete it - but I actually probably would rather have it just create the file new each time I run, so that I can still see the output as I work out my other problem, but not have to clear it manually each time. I see there is a createNewFile method, but it doesn't seem to do what I'm looking for or I'm not using it correctly. – user3259647 Feb 02 '14 at 01:12
  • Your statement above doesn't make sense. Please clarify. – Hovercraft Full Of Eels Feb 02 '14 at 01:17
  • I've already used File to create the file, and then put that in to a FileWriter as you suggested, the delete does then work - but what I am actually needing is to just have the file cleared when the program runs rather than delete the file. – user3259647 Feb 02 '14 at 01:20

2 Answers2

2

If you don't want to append to the file then use the correct FileWriter constructor parameter values for the second parameter, the "append" parameter:

Not:

fileWriter = new FileWriter(file, true);

but rather:

fileWriter = new FileWriter(file, false);

As usual, please check the FileWriter API for the details.


Edit
You state:

I am probably just confusing myself too much at this point - when I set append to false, it replaces what was in the file in the last run of my loop, so I end up with just the last line - well I want to feed the data in line by line while the program runs, when it finishes I want the file to be left there, until the next run - at that point I want to clear the contents..

It sounds like you're creating a new FileWriter with each iteration of the loop. If so, don't. Create one FileWriter with the second constructor parameter set to false, and create it before your loop. Then loop with it, without re-creating it in the loop, and then when the loop is over and you're through using it, close it. Simplify things.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I want to append to the file the data I get from the input so I can then see the contents of the output file - but then I want that same file to just be cleared on the next run. – user3259647 Feb 02 '14 at 01:37
  • `"I want that same file to just be cleared on the next run."` -- I'm quite confused again. Your statement above means not appending to file. – Hovercraft Full Of Eels Feb 02 '14 at 01:48
  • I am probably just confusing myself too much at this point - when I set append to false, it replaces what was in the file in the last run of my loop, so I end up with just the last line - well I want to feed the data in line by line while the program runs, when it finishes I want the file to be left there, until the next run - when I next run I want to clear the contents.. – user3259647 Feb 02 '14 at 02:01
  • @user3259647: yes, you're over-complicating things. Please read edit to answer. – Hovercraft Full Of Eels Feb 02 '14 at 02:05
  • I think I got it, I checked for the file size to be greater than zero before I start processing the input file and then at that point set append to false and write an empty string, then I am able to get all of the data represented on each run as I am looking for by setting append back to true while I am writing the data. Not elegant, but it works for now. Thanks for the help :) – user3259647 Feb 02 '14 at 02:08
  • @user3259647: good deal. – Hovercraft Full Of Eels Feb 02 '14 at 02:09
0

This will work for deleting a file when you want to (in most cases- you must have sufficient permissions), but you have to call the method. Otherwise, if you just want to delete the file when the JVM closes, use file.deleteOnExit(), catching the same security error.

public void delete(File file){

    try{
        if(file.delete()){
               Logger.info("File deleted");
            }
            else{
               Logger.warning("Deletion Failure");
       }

    }
    catch(SecurityException e){
          Logger.warning("Insufficient Permissions");
    }
}
erzr2
  • 155
  • 3
  • 12