关于createTempFile()与deleteOnExit()方法
这个程序的目的是把键盘输入的字节先存到临时文件letter.txt里面,然后再从里面读出来,写放line.txt文件里,结果读写没问题,就是临时文件letter.txt无法删除!!这是什么原因??
import java.io.*;
public class TestFile {
public static void main(String[] args) {
int b,c;
byte[] buffer = new byte[100];
File f = new File( "G:\\com.gknl ");
File fTemp = null;
try {
fTemp=File.createTempFile( "letter ", ".txt ",f);
System.out.println( "输入一行文本,并存入磁盘: ");
b = System.in.read(buffer);
FileOutputStream writeFile = new FileOutputStream(fTemp);
writeFile.write(buffer,0,b);
FileInputStream In = new FileInputStream(fTemp);
FileOutputStream Out = new FileOutputStream( "G:\\com.gknl\\line.txt ");
while((c=In.read())!=-1){
Out.write(c);
}
fTemp.deleteOnExit();
System.out.println( "输入完毕!! ");
} catch (
IOException e1) {
e1.printStackTrace();
}
}
}
------解决方案--------------------In.close();
writeFile.close();
Out.close();
我试过了,可以