0

My project requires changing of a particular file contents. I need to create a new everytime the code is executed, irrespective whether the file exists or not. If the file exists, then the original file's content should be deleted and only the new/fresh contents should be present in the file.

Note: The file name and file location will be same in every execution.

Code:

Spring.xml:

<bean id=".." class="ImageDataProcess">
    <constructor-arg ref="x.y.z.filepath.newImageDataFilepath" />
    <other constructor args>
</bean>

<bean id="x.y.z.filepath.newImageDataFilepath">
     <constructor-arg value="#{tmpDirectoryPath}/image-data.txt" />
</bean>

ImageDataProcess.java:

@RequiredArgsConstructor
public class ImageDataProcess implements PQR {
  private final String newImageDataTextFilepath;
  //other values coming from spring
      @Override
      public void execute() {
          File f = new File(newImageDataTextFilepath);
          ://logic here
          :
       Forloop(){
          FileUtils.writeStringToFile(f, imageDataFileLine + NEWLINE, true);
        }
    }
}

But this just appends the new contents in the old file. As of now, if I run two iterations back to back, the file size becomes just double as it was before, but i want it to be the same, even if data is same. Any one knows how can I achieve this??

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
user2696258
  • 1,159
  • 2
  • 14
  • 26
  • a quick look into the api might help. Check what you are passing there and what i causes. Hint: one of these parameters tells the program to append or to not append. As another sidenode: [following the FileUtils Api the call you are doing is depreceated](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/deprecated-list.html) – SomeJavaGuy Oct 19 '16 at 14:42
  • Check if it exists, if it does delete it and recreate? You could even store the old data temp just in case you need it for whatever reason. – Deckerz Oct 19 '16 at 14:43
  • @Kevin: yeah I know it is for append. But its inside the for loop so I need to append there. I want a new empty file with same name before the forloop begins. – user2696258 Oct 19 '16 at 14:45
  • @Deckerz: I also thought that but I don't know if its a good practise. Can we have anything else for this? – user2696258 Oct 19 '16 at 14:46
  • 2
    Possible duplicate of [Writing to a new file with erasing previous data Java](http://stackoverflow.com/questions/40127972/writing-to-a-new-file-with-erasing-previous-data-java) – Zachary Craig Oct 19 '16 at 15:01
  • 2
    Literally the EXACT same question, actually, same code, different names. Are we trying to help with someone's homework or something? – Zachary Craig Oct 19 '16 at 15:02
  • @zack6849 Nope, same user too. They posted the question twice. – walen Oct 19 '16 at 15:03
  • My bad, i guess i can't read usernames. – Zachary Craig Oct 19 '16 at 15:04
  • Maybe we should flag the other one for removal, since this one has more answers ? – walen Oct 19 '16 at 15:05
  • No its not a homework. I posted the other question twice. If you look at the code, its a bit different. I am trying possible solutions. Thought as another question to reduce confusion here. – user2696258 Oct 20 '16 at 06:44
  • hey I have found a way of doing it. using Printwriter class. But the test case is not passing. please help, if u can. I have posted the question here: http://stackoverflow.com/questions/40173392/wanted-but-not-invoke-mockito-print-writer – user2696258 Oct 21 '16 at 09:50

2 Answers2

0
FileUtils.writeStringToFile(f, imageDataFileLine + NEWLINE, true);

You need to remove the boolean "true" here. This specifies to append to the file instead of overwriting.

I've linked to the specific method in the api.

Update: You could also delete the file before writing to it. If you need to append.

FileUtils.forceDelete(file);
Brion
  • 746
  • 3
  • 10
  • yeah I know it is for append. But its inside the for loop so I need to append there. I want a new empty file with same name before the forloop begins. – user2696258 Oct 19 '16 at 14:48
  • What could be the possible solution for this now? – user2696258 Oct 19 '16 at 14:49
  • You can delete the file before writing. Then if you're only writing in the loop it will create a new file for you on its first iteration. I've updated the answer to reflect this. – Brion Oct 19 '16 at 14:53
  • hey I have found a way of doing it. using Printwriter class. But the test case is not passing. please help, if u can. I have posted the question here: http://stackoverflow.com/questions/40173392/wanted-but-not-invoke-mockito-print-writer – user2696258 Oct 21 '16 at 09:50
0

What about just using a variable that you do pass to the method. Just initialize it with false, and set it to true after the first writing operation

boolean append = false;
File f = new File(path);
...
for(...){
    FileUtils.writeStringToFile(f, imageDataFileLine + NEWLINE, append);
    if(!append) append = true;
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • hey.. yeah this could work. I am skeptical on the part that, if I have the file originally with 10 lines, and then the new contents to be written on the file are just 6 lines. Will the final file have the last 4 lines? [FYI: I donot want these 4 lines]. – user2696258 Oct 19 '16 at 18:01
  • hey I have found a way of doing it. using Printwriter class. But the test case is not passing. please help, if u can. I have posted the question here: http://stackoverflow.com/questions/40173392/wanted-but-not-invoke-mockito-print-writer – user2696258 Oct 21 '16 at 09:50