📜  如何在Java删除临时文件?

📅  最后修改于: 2022-05-13 01:54:27.845000             🧑  作者: Mango

如何在Java删除临时文件?

在Java,我们有一个Java.io 包,它提供了各种方法来对文件/目录执行各种操作。

临时文件是为某些业务逻辑或单元测试创建的文件,使用这些文件后,您必须确保应删除这些临时文件。如果您忘记删除这些临时文件,那么这将在执行时占用额外的空间,这对您的应用程序来说效率不高。

创建临时文件: - Java.io 包中存在的文件类具有 createTempFile() 方法,该方法将采用两个参数,即文件名(前缀)和临时文件的扩展名(后缀)。提供您需要在其中创建临时文件的抽象路径。

Java
// Create temp file
 
import java.io.File;
import java.io.IOException;
public class tempFile {
    public static void main(String args[])
        throws IOException
    {
        // name of the file
        String prefix = "TempFile";
 
        // extension of the file
        String suffix = ".txt";
 
        // Creating a File object for directory
        File directoryPath = new File(
            "/home/krishna/Desktop/java/junior/");
 
        // Creating a temp file
        File.createTempFile(prefix, suffix, directoryPath);
        System.out.println(
            "Temp file created at the specified path");
    }
}


Java
// Delete File when JVM Exists
 
import java.io.File;
import java.io.IOException;
 
public class tempFile {
    public static void main(String[] args)
    {
        File temp;
        try {
            // name of the file and extension
            temp = File.createTempFile("TempFile", ".txt");
 
            // Delete when JVM exits
            temp.deleteOnExit();
        }
        // If not found any temporary file
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Java
// Delete file using delete() method
 
import java.io.File;
import java.io.IOException;
 
public class tempFile {
    public static void main(String[] args)
    {
        File temp;
        try {
            temp
                = File.createTempFile("myTempFile", ".txt");
 
            // Perform other operations
            // Delete the file immediately
            temp.delete();
        }
        // If not found the temporary file
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}


输出:

Temporary file has been created in the specified path



有两种方法可以删除现有的临时文件。

A. JVM退出时删除:

仅当应用程序完成时,您才能使用 deleteOnExit() 方法删除现有文件。如果您的代码异常终止,请记住您的临时文件还没有被删除,一旦您请求删除操作,之后就无法取消。

Java

// Delete File when JVM Exists
 
import java.io.File;
import java.io.IOException;
 
public class tempFile {
    public static void main(String[] args)
    {
        File temp;
        try {
            // name of the file and extension
            temp = File.createTempFile("TempFile", ".txt");
 
            // Delete when JVM exits
            temp.deleteOnExit();
        }
        // If not found any temporary file
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

B. 立即删除文件:

我们可以使用 use delete() 方法删除临时文件。

Java

// Delete file using delete() method
 
import java.io.File;
import java.io.IOException;
 
public class tempFile {
    public static void main(String[] args)
    {
        File temp;
        try {
            temp
                = File.createTempFile("myTempFile", ".txt");
 
            // Perform other operations
            // Delete the file immediately
            temp.delete();
        }
        // If not found the temporary file
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}