在创建文件之前,删除前一天的文件

Kis*_*mar 1 java file

我正在C:驱动器文件夹abc中创建一个dat文件,如下所示,现在我的文件每天生成,现在假设如果今天生成我的文件,那么将来也会像往常一样生成它,但是当生成它时,我必须请确保删除较早的文件,因为该文件夹中的空间有限,并且每次都要进行此检查,然后才从该文件夹中删除该文件,请告知如何实现。

File file = new File(FilePath + getFileName()); //filepath is being passes through //ioc         //and filename through a method 


        if (!file.exists()) {
            file.createNewFile();
        }

FileOutputStream fileOutput = new FileOutputStream(
                file);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                fileOutput));
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 6

为什么不使用file.delete()

File file = new File(FilePath + getFileName()); //filepath is being passes through //ioc         //and filename through a method 

if (file.exists()) {
     file.delete(); //you might want to check if delete was successfull
}
file.createNewFile();

FileOutputStream fileOutput = new FileOutputStream(file);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutput));
Run Code Online (Sandbox Code Playgroud)