createNewFile()和createTempFile()区别:
为了更好地测试,我建了两个类:
1、使用createNewFile()创建一个abc.txt的文件:
Java代码- public class TestFile1 {
- public static voidmain(String[] args) {
- File f1 = newFile("C:\abc.txt");
- try{
- f1.createNewFile();
- System.out.println(f1.getName());
- } catch (IOExceptione) {
- e.printStackTrace();
- }
- }
- }
public class TestFile1 { public static void main(String[] args) { File f1 = new File("C:\abc.txt"); try { f1.createNewFile(); System.out.println(f1.getName()); } catch (IOException e) { e.printStackTrace(); } }}
控制台输出:
abc.txt
2、使用createTempFile()创建一个abc.txt的文件:
Java代码- public class TestFile2 {
- public static voidmain(String[] args) {
- File f1 = newFile("C:");
- File f2 = null;
- try{
- f2 = File.createTempFile("abc", ".txt", f1);
- System.out.println(f2.getName());
- } catch (IOExceptione) {
- e.printStackTrace();
- }
- }
- }
public class TestFile2 { public static void main(String[] args) { File f1 = new File("C:"); File f2 = null; try { f2 = File.createTempFile("abc", ".txt", f1); System.out.println(f2.getName()); } catch (IOException e) { e.printStackTrace(); } }}
控制台输出:
空
但是我查看了指定路径,生成了
abc482578709 1196303263.txt文件,每一次执行,都能生成不同的文件,但中间的数字都是19位,我查看了Java的File源代码,按住Ctrl+鼠标左击,进入File.class,看到有
Java代码- private static File generateFile(String prefix,String suffix, File dir)
- throwsIOException
- {
- long n =LazyInitialization.random.nextLong();
- if (n ==Long.MIN_VALUE) {
- n = 0;// corner case
- } else{
- n = Math.abs(n);
- }
- return new File(dir, prefix + Long.toString(n) +suffix);
- }
private static File generateFile(String prefix, String suffix, File dir) throws IOException { long n = LazyInitialization.random.nextLong(); if (n == Long.MIN_VALUE) { n = 0; // corner case } else { n = Math.abs(n); } return new File(dir, prefix + Long.toString(n) + suffix); }Java代码
- public static File createTempFile(String prefix,String suffix,
- File directory)
- throwsIOException
- {
- if (prefix ==null) throw newNullPointerException();
- if (prefix.length()< 3)
- throw newIllegalArgumentException("Prefixstring too short");
- String s = (suffix == null) ? ".tmp" : suffix;
- if (directory== null){
- String tmpDir =LazyInitialization.temporaryDirectory();
- directory = newFile(tmpDir, fs.prefixLength(tmpDir));
- }
- SecurityManager sm =System.getSecurityManager();
- File f;
- do{
- f = generateFile(prefix, s, directory);
- } while(!checkAndCreate(f.getPath(), sm));
- returnf;
- }
public static File createTempFile(String prefix, String suffix, File directory) throws IOException { if (prefix == null) throw new NullPointerException(); if (prefix.length() < 3) throw new IllegalArgumentException("Prefix string too short"); String s = (suffix == null) ? ".tmp" : suffix; if (directory == null) { String tmpDir = LazyInitialization.temporaryDirectory(); directory = new File(tmpDir, fs.prefixLength(tmpDir)); } SecurityManager sm = System.getSecurityManager(); File f; do { f = generateFile(prefix, s, directory); } while (!checkAndCreate(f.getPath(), sm)); return f; }
注意函数generateFile()的返回值是new File(dir, prefix + Long.toString(n) +suffix);
由此可明白为什么会生成abc4825787091196303263.txt文件了。