File类静态文件createTempFile(String file_name,String file_extension) (File Class static File createTempFile(String file_name, String file_extension))
This method is available in package java.io.File.createTempFile(String file_name , String file_extension).
软件包java.io.File.createTempFile(String file_name,String file_extension)中提供了此方法。
This method is used to create a temporary file in a specified directory and if the directory is not given then default directory will get selected.
此方法用于在指定目录中创建一个临时文件,如果未提供该目录,则将选择默认目录。
This method is static so it will be accessible with classname.
该方法是静态的,因此可以使用类名进行访问。
Syntax:
句法:
static File createTempFile(String file_name , String file_extension){
}
Parameter(s):
参数:
We pass two objects as a parameter in the method of the File i.e file_name and file_extension. So the first argument is filename and the second argument is file extension so if the file extension is null then it takes default extension is '.tmp'.
我们在File方法中传递两个对象作为参数,即file_name和file_extension。 因此,第一个参数是filename,第二个参数是文件扩展名,因此,如果文件扩展名为null,则默认扩展名为'.tmp' 。
Return value:
返回值:
The return type of this method is File. So, it returns a temporary file.
该方法的返回类型为File 。 因此,它返回一个临时文件。
Java程序,演示createTempFile()方法的示例 (Java program to demonstrate example of createTempFile() method)
// import the File class because we will use File class methods
import java.io.File;
// import the Exception class because it may raise an
// exception when working with files
import java.lang.Exception;
public class CreateTempFile {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes to
// escape '\' character sequence for windows otherwise
// it will be considerable as url.
// create a temporary file object
File file = File.createTempFile("Java", ".txt");
// check whether file exists or not
if (file.exists()) {
// Temporary file will create in current directory with named
// Java and extension is .txt so the filename will be Java.txt
System.out.println("Temp File created: " + file.getName());
} else {
// if the file is not created
System.out.println("Temp File cannot be created: " + file.getName());
}
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
E:\Programs>javac CreateTempFile.java
E:\Programs>java CreateTempFile
Temp File created: Java5580937140195842517.txt