日期:2014-05-20  浏览次数:21109 次

一个很简单的问题,关于File类的createTempFile()方法
原本应该是再简单不过的方法调用,但是却出现了意外的结果,搞的和头大。主要是我想调用createTempFile来创建一个文件Test.txt,但是文件创建出来后,会在名字中参杂4-5个随机数字,我试了几种方法,还是老样子这才发上来问问大家。
Java code
        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);
        }


------解决方案--------------------
没遇过
------解决方案--------------------
创建文件应该用new File(path).createNewFile(),File.createTempFile()是用来创建临时文件的,你给的参数Test只是一个前缀,不是文件名。好好看看JavaDoc
------解决方案--------------------
up
------解决方案--------------------
楼主不读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]

该方法保证创建出来的文件在方法调用前不存在,而且任何两次调用都不会返回相同的路径。如果它不加随机数,怎么保证这两点?