import java.io.*;
public class CreateTempFile {
public static void main(String[] args) throws IOException {
File temp = File.createTempFile("pattern","suffix"); //为临时文件名称添加前缀和后缀
System.out.println("临时文件名称为:"+temp.getName());
temp.deleteOnExit(); //在退出时删除
/**
* 在临时文件中写入内容
*/
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp));
bufferedWriter.write("这是一个临时文件 !!!");
bufferedWriter.close();
/**
* 读取临时文件中的内容
*/
BufferedReader bufferedReader = new BufferedReader(new FileReader(temp));
String string;
while ((string=bufferedReader.readLine()) != null){
System.out.println(string);
}
bufferedReader.close();
System.out.println("临时 文件 创建 成功!!");
}
}