原本应该是再简单不过的方法调用,但是却出现了意外的结果,搞的和头大。主要是我想调用createTempFile来创建一个文件Test.txt,但是文件创建出来后,会在名字中参杂4-5个随机数字,我试了几种方法,还是老样子这才发上来问问大家。
File file2 = new File("D:\\333\\Test.txt");
File file3 = new File("D:\\333\\");
// String s = "Test";
if(!file2.isFile()){
File.createTempFile("Test", ".txt", file3);
// File.createTempFile("Test.", ".txt", file3);
// File.createTempFile("Test.", "txt", file3);
// File.createTempFile(s, ".txt", file3);
}

解决方案 »

  1.   

    创建文件应该用new File(path).createNewFile(),File.createTempFile()是用来创建临时文件的,你给的参数Test只是一个前缀,不是文件名。好好看看JavaDoc
      

  2.   

    回复楼上的,我是故意用这个createTempFile来建文件的,只不过它没说用这个方法建会出现这样现象。难道这个方法就是这样的?(建完后会自动加随机数?)
      

  3.   

    楼主不读JavaDoc么?
    Quote:

    If this method returns successfully then it is guaranteed that: 1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and 
    2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
    Quote:
    该方法保证创建出来的文件在方法调用前不存在,而且任何两次调用都不会返回相同的路径。如果它不加随机数,怎么保证这两点?
      

  4.   

    If this method returns successfully then it is guaranteed that:  1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and  
    2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.要读API要读API