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??