Java File类静态文件createTempFile(String file_name,String file_extension)方法,带有示例...

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


翻译自: https://www.includehelp.com/java/file-class-static-file-createtempfile-string-file_name-string-file_extension-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值