我正在开发一个项目,并通过spring(beans)设置了文件对象,并使用Lombok的RequiredArgsConstructor进行了设置。

码:

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。

我也之前和之后的文件内容。它有重复项。

我知道它与bean和spring有关,就像仅形成一个实例一样。但问题是,我还希望执行后的该文件也可以在下一步中上传到服务器上。

另外,在循环中,我需要附加数据。但是,在循环开始之前,请打开一个新文件以进行覆盖。

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


请告诉我如何才能解决此问题??它会全部工作吗?
我有什么方法可以做到这一点?

我是Spring和所有人的新手。
任何帮助表示赞赏。

最佳答案

根据FileUtils文档here

只需将函数调用更改为

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

真正的布尔值明确告诉实用程序只是将其追加到文件中,而不是覆盖它

09-30 14:11
查看更多