本例以Windows, JDK1.7 为基础,来演示使用Java 创建临时文件。
创建临时文件
FileCreateTemporary.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.devnp.io;
import java.io.File; import java.io.IOException;
public class FileCreateTemporary {
public static void main(String[] args) throws IOException { createTempfile(); createTempfileWithDir(); }
public static void createTempfile() throws IOException{ File file = File.createTempFile("2017-04-12-file-", ".temp"); System.out.println("Temp File Path : " + file.getAbsolutePath()); }
public static void createTempfileWithDir() throws IOException{ File file = File.createTempFile("2017-04-12-file-", ".temp", new File("C:\\")); System.out.println("Temp File Path : " + file.getAbsolutePath()); }
}
|
运行结果:
1 2
| Temp File Path : C:\Users\duliu\AppData\Local\Temp\2017-04-12-file-4637949104986473884.temp Temp File Path : C:\2017-04-12-file-6496576605368356314.temp
|
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE