java创建临时文件_用Java创建一个临时文件
java創(chuàng)建臨時(shí)文件
The task is to create a temporary file in Java.
任務(wù)是用Java創(chuàng)建一個(gè)臨時(shí)文件。
Creating a temporary file
創(chuàng)建一個(gè)臨時(shí)文件
To create a temporary file in java – we use createTempFile() method of "File" class. The createTempFile() method is called with the File object which should be initialized with the file name (and path if you want to specify).
要在Java中創(chuàng)建一個(gè)臨時(shí)文件,我們使用“ File”類(lèi)的createTempFile()方法 。 用File對(duì)象調(diào)用createTempFile()方法 ,該對(duì)象應(yīng)使用文件名(和路徑,如果要指定)初始化。
The createTempFile() method returns a File object, and by using getName() method, we can access the temporary file name.
createTempFile()方法返回一個(gè)File對(duì)象,通過(guò)使用getName()方法 ,我們可以訪問(wèn)臨時(shí)文件名。
This method accepts two arguments 1) file_name and 2) file_extension name. If the file_extension is null, the default file extension will be .tmp.
此方法接受兩個(gè)參數(shù)1) file_name和2) file_extension name。 如果file_extension為null ,則默認(rèn)文件擴(kuò)展名為.tmp 。
Package to use: import java.io.*;
要使用的包: import java.io. *;
Syntax:
句法:
//file object creationFile one = null;File two = null;//temporary file creationone = File.createTempFile("file_name", "extension");two = File.createTempFile("file_name", "extension");Java代碼創(chuàng)建一個(gè)臨時(shí)文件 (Java code to create a temporary file)
// Java code to create a temporary file import java.io.*;public class Main {public static void main(String[] args) {//file object creation to store resultsFile one = null;File two = null;try {//creating temporary file1 with extension '.javatemp'one = File.createTempFile("sample1", ".javatemp");//creating temporary file2 without using nulltwo = File.createTempFile("sample2", null);System.out.println("file created: " + one.getPath());System.out.println("file created: " + two.getPath());} catch (IOException exp) {System.out.println("Error in creating temporary file: " + exp);}} }Output
輸出量
file created: /tmp/sample14223766203657377545.javatemp file created: /tmp/sample22708977934903691208.tmp翻譯自: https://www.includehelp.com/java-programs/create-a-temporary-file-in-java.aspx
java創(chuàng)建臨時(shí)文件
總結(jié)
以上是生活随笔為你收集整理的java创建临时文件_用Java创建一个临时文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 房车租赁一天多少钱啊?
- 下一篇: java treemap_Java Tr