0

I am working on a project, and having file object set through spring (beans), and using RequiredArgsConstructor of Lombok to do so.

Code:

Spring.xml:

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

ImageDataProcess.java:

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

This image-data file is getting formed, but everytime I execute the file the new contents are getting appended. But I want only the new contents in the file and old contents to be erased.

So for example, I already have this file made by one execution of the program as image-data.txt of 2.7 MB. When I execute this program the another time, it makes this file as image-data.txt as 5.4 MB.

I also the content of the files before and after. It has duplicates.

I know it has something to do with beans and spring, like only one instance is getting formed. But the thing is I want this file after the execution also, to be uploaded on a server, in the next step.

Also, in the loop I need to append the data. But make a new file opened for over writing before the loop begins.

Fix:
Can I do something like have the file path from the bean(as this is always the same), but create a new file in the java file using "new File(filepath)". Will this override the existing file always?? Even if the file with the same name is present at the same location??

Please tell me how I can achieve this fix?? and will it at all work?? What are the ways I can do this??

I am novice to springs and all. Any help is appreciated.

user2696258
  • 1,159
  • 2
  • 14
  • 26

1 Answers1

1

As per the FileUtils documentation here

Just change your function call to

FileUtils.writeStringToFile(newImageDataTextFile, imageDataFileLine + NEWLINE, false);

The true boolean there is explicitly telling the utility to just append to the file, rather than overwrite it

Zachary Craig
  • 2,192
  • 4
  • 23
  • 34
  • 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:53
  • What could be another solution now? Does java even allow such a thing? – user2696258 Oct 19 '16 at 14:53
  • Then just delete the file if it exists before your for loop :) The first iteration of the for loop will create the file (assuming that writeStringToFile method creates the file if it doesn't exist, if not, you'll have to manually create the file), then append to it. So every time your method is called, it will erase the old file, create a new one, and do the appending on that. – Zachary Craig Oct 19 '16 at 14:54
  • 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