我正在处理项目,并通过spring(beans)设置文件对象,并使用Lombok的RequiredArgsConstructor来完成此操作。使用删除以前的数据写入新文件Java
代码:
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);
}
}
}
该图像数据文件被越来越形成,但每次我执行新的内容越来越附加文件。但是我只想将文件中的新内容和旧内容删除。
因此,例如,我已经通过程序的一次执行将此文件制作为2.7 MB的image-data.txt文件。 当我另一次执行这个程序时,它将这个文件作为image-data.txt作为5.4 MB。
我也是之前和之后的文件内容。它有重复。
我知道它与豆类和春天有关,就像只有一个实例正在形成。但事情是,我想在执行后还要将这个文件上传到服务器上,下一步。
另外,在循环中我需要追加数据。但是在循环开始之前打开一个新的文件以便重写。
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??
请告诉我如何才能实现此修复?并会在一切工作? 我能做到这一点的方式是什么?
我是新手和泉水。 任何帮助表示赞赏。
在bean中将bean的范围设置为prototype scope =“prototype”。 –
这不能解决问题:( – user2696258