📜  Java中的文件 createTempFile() 方法及示例

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

Java中的文件 createTempFile() 方法及示例

createTempFile()函数在给定目录中创建一个临时文件(如果未提及目录,则选择默认目录),该函数使用作为参数传递的前缀和后缀生成文件名。如果后缀为空,则函数使用“.tmp”作为后缀。然后该函数返回创建的文件
函数签名:

句法:

1. File.createTempFile(String, String, FILE);

2. File.createTempFile(String, String);

参数:该函数是一个重载函数,因此一个函数接受后缀、前缀和一个文件对象,而另一个函数只接受后缀和前缀。前缀不得少于三个字符,但后缀可能为空,如果目录不是指定或传递空值,则函数使用默认目录。
返回类型:函数返回表示新创建的临时文件的抽象文件名
异常:此方法抛出:

  • IllegalArgumentException:如果前缀参数包含少于三个字符
  • IOExcetion:如果有任何 IO 错误(无法创建文件)
  • SecurityException:如果方法不允许创建文件

下面的程序说明了 createTempFile()函数的使用:
示例1:如果我们提供前缀字符串并提供空后缀字符串

Java
// Java program to demonstrate
// createTempFile() method of File Class
 
import java.io.*;
 
public class solution {
    public static void main(String args[])
    {
 
        try {
            // create a temp file
            File f
                = File.createTempFile("geeks", null);
 
            // check if the file is created
            if (f.exists()) {
 
                // the file is created
                // as the function returned true
                System.out.println("Temp File created: "
                                   + f.getName());
            }
 
            else {
 
                // display the file cannot be created
                // as the function returned false
                System.out.println("Temp File cannot be created: "
                                   + f.getName());
            }
        }
 
        catch (Exception e) {
 
            // display the error message
            System.err.println(e);
        }
    }
}


Java
// Java Program to demonstrate
// createTempFile() method
 
import java.io.*;
 
public class solution {
 
    public static void main(String args[])
    {
        try {
 
            // create a temp file
            File f = File.createTempFile("geeks", ".SVP");
 
            // check if the file is created
            if (f.exists()) {
 
                // the file is created
                // as the function returned true
                System.out.println("Temp File created: "
                                   + f.getName());
            }
 
            else {
 
                // display the file cannot be created
                // as the function returned false
                System.out.println("Temp File cannot be created: "
                                   + f.getName());
            }
        }
 
        catch (Exception e) {
 
            // display the error message
            System.err.println(e);
        }
    }
}


Java
// Java Program to demonstrate
// createTempFile() method
 
import java.io.*;
 
public class solution {
 
    public static void main(String args[])
    {
        try {
            // create a temp file
            File f = File.createTempFile("geeks",
                                         ".SVP",
                                         new File("F:"));
 
            // check if the file is created
            if (f.exists()) {
 
                // the file is created
                // as the function returned true
                System.out.println("Temp File created: "
                                   + f.getAbsolutePath());
            }
 
            else {
 
                // display the file cannot be created
                // as the function returned false
                System.out.println("Temp File cannot be created: "
                                   + f.getAbsolutePath());
            }
        }
 
        catch (Exception e) {
 
            // display the error message
            System.err.println(e);
        }
    }
}


输出:

示例 2:如果我们提供前缀字符串和明确的后缀字符串

Java

// Java Program to demonstrate
// createTempFile() method
 
import java.io.*;
 
public class solution {
 
    public static void main(String args[])
    {
        try {
 
            // create a temp file
            File f = File.createTempFile("geeks", ".SVP");
 
            // check if the file is created
            if (f.exists()) {
 
                // the file is created
                // as the function returned true
                System.out.println("Temp File created: "
                                   + f.getName());
            }
 
            else {
 
                // display the file cannot be created
                // as the function returned false
                System.out.println("Temp File cannot be created: "
                                   + f.getName());
            }
        }
 
        catch (Exception e) {
 
            // display the error message
            System.err.println(e);
        }
    }
}

输出:

Temp File created: geeks4425780422923344328.SVP

示例 3:如果我们提供前缀字符串、明确的后缀字符串和目录

Java

// Java Program to demonstrate
// createTempFile() method
 
import java.io.*;
 
public class solution {
 
    public static void main(String args[])
    {
        try {
            // create a temp file
            File f = File.createTempFile("geeks",
                                         ".SVP",
                                         new File("F:"));
 
            // check if the file is created
            if (f.exists()) {
 
                // the file is created
                // as the function returned true
                System.out.println("Temp File created: "
                                   + f.getAbsolutePath());
            }
 
            else {
 
                // display the file cannot be created
                // as the function returned false
                System.out.println("Temp File cannot be created: "
                                   + f.getAbsolutePath());
            }
        }
 
        catch (Exception e) {
 
            // display the error message
            System.err.println(e);
        }
    }
}

输出:

Temp File created: F:\geeks7006753451952178741.SVP

注意:这些程序可能无法在在线 IDE 中运行。请使用离线 IDE 并设置文件路径。