本例以Windows, JDK1.7 为基础,来演示使用Java 写入信息到临时文件。

写入信息到临时文件

FileWriteTemp.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
package com.devnp.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteTemp {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

File file = File.createTempFile("2017-04-12-file-", ".temp");

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));

bufferedWriter.write("This is temp file.");

bufferedWriter.close();

System.out.println("Temp File Path : " + file.getAbsolutePath());


}

}
运行结果:
1
Temp File Path : C:\Users\duliu\AppData\Local\Temp\2017-04-12-file-7784522935596545481.temp