Java 创建文件前,删除前一天的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18373297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:58:23  来源:igfitidea点击:

Before creating the file , deleting the file of previous day

javafile

提问by Kishan Kumar

I am creating a dat file in C: drive folder named abc as shown below , Now my file is generated everyday now suppose if my file is generated today, then tommrow it will be also generated as usual but when tommrow it is generated I have to make sure that earlier day file is deleted as the space in that folder is limited and this check is every time need to be done previos day file to be get deleted from that folder , please advise how to achieve this..

我正在 C: 驱动器文件夹中创建一个名为 abc 的 dat 文件,如下所示,现在我的文件每天都会生成,现在假设我的文件是今天生成的,那么 tommrow 它将照常生成但是当 tommrow 生成时我必须确保删除较早的文件,因为该文件夹中的空间有限,并且每次需要完成此检查才能从该文件夹中删除前一天的文件,请告知如何实现此目的..

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));

回答by Ashwani

If you are using Java 7 then there is standard way to get file creation time, So that you can check if file is created in previous day and should be delete.

如果您使用的是 Java 7,那么有标准的方法来获取文件创建时间,以便您可以检查文件是否在前一天创建并且应该删除。

    Path path = Paths.get("/filepath/");
    BasicFileAttributes fileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
    System.out.println("creationTime:"+ fileAttributes.creationTime());

回答by Alex

why not use file.delete()?

为什么不使用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));

回答by Ruchira Gayan Ranaweera

If your file name same in time to time no need to delete that. By running your code tomorrow, will over write file created today.

如果您的文件名不时相同,则无需删除该文件。通过明天运行您的代码,将覆盖今天创建的文件。

Consider following case

考虑以下情况

    BufferedWriter bw=new BufferedWriter(new FileWriter("D:\Test\test.txt"));
    bw.write("abbbb");
    bw.close();  // now this will create a test.txt in side Test folder

now run this by change writing String

现在通过更改写入字符串来运行它

    BufferedWriter bw=new BufferedWriter(new FileWriter("D:\test.txt"));
    bw.write("hihi");
    bw.close(); // now you can see file only containing hihi

回答by Saeed

You can change your code this way:

您可以通过以下方式更改代码:

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

And if it does not work, it's a matter of permission.

如果它不起作用,那就是许可问题。