createTempFile()函數在給定目錄中創建一個臨時文件(如果未提及目錄,則選擇默認目錄),該函數使用作為參數傳遞的前綴和後綴生成文件名。如果後綴為null,則該函數使用“.tmp”作為後綴。然後該函數返回創建的文件
函數簽名:
1. public static File createTempFile(String PREFIX, String SUFFIX)
OR
2. public static File createTempFile(String PREFIX, String SUFFIX, File DIR)
用法:
1. File.createTempFile(String, String, FILE); 2. File.createTempFile(String, String);
參數:該函數是一個重載函數,因此一個函數帶有後綴,前綴和File對象,而另一個函數僅帶有後綴和前綴。前綴不得少於三個字符,但後綴可以為null且如果未指定目錄,則為傳遞一個空值,然後該函數使用默認目錄。
返回類型:該函數返回表示新創建的臨時文件的抽象文件名
異常:該方法拋出:
- IllegalArgumentException:如果prefix參數包含少於三個字符
- IOExcetion:是否有IO錯誤(無法創建文件)
- SecurityException:如果該方法不允許創建文件
以下示例程序旨在說明createTempFile()函數的用法:
範例1:如果我們提供前綴字符串並提供空後綴字符串
// 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 retruned 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: geeks7503529537487244659.tmp
範例2:如果我們提供前綴字符串和後綴字符串
// 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 retruned 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 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 retruned 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並設置文件的路徑。
相關用法
- Java File isHidden()用法及代碼示例
- Java File getAbsolutePath()用法及代碼示例
- Java File length()用法及代碼示例
- Java File delete()用法及代碼示例
- Java File getName()用法及代碼示例
- Java File getFreeSpace()用法及代碼示例
- Java File getAbsoluteFile()用法及代碼示例
- Java File lastModified()用法及代碼示例
- Java File createNewFile()用法及代碼示例
- Java File getCanonicalPath()用法及代碼示例
- Java File isFile()用法及代碼示例
- Java File exists()用法及代碼示例
- Java File getTotalSpace()用法及代碼示例
- Java File setExecutable()用法及代碼示例
- Java File mkdir()用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 File createTempFile() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。