当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java File createTempFile()用法及代码示例


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并设置文件的路径。



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 File createTempFile() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。