Java在指定目录中创建文件,为什么输出不是C:\JavaTemp.javatemp? 文件名后面的数字是什么意思?求指点。
importjava.io.File;publicclassMain{publicstaticvoidmain(String[]args)throwsException{...
import java.io.File;
public class Main {
public static void main(String[] args)
throws Exception {
File file = null;
File dir = new File("d:/");
file = File.createTempFile
("JavaTemp", ".javatemp", dir);
System.out.println(file.getPath());
}
}
文件名后面的数字是什么意思?为什么会生成这样?
输出结果如下:
已解决。 展开
public class Main {
public static void main(String[] args)
throws Exception {
File file = null;
File dir = new File("d:/");
file = File.createTempFile
("JavaTemp", ".javatemp", dir);
System.out.println(file.getPath());
}
}
文件名后面的数字是什么意思?为什么会生成这样?
输出结果如下:
已解决。 展开
2个回答
展开全部
这种问题最直接最好的办法直接看源码:
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;
}
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);
}
然后那串数字生成的源头:
return ((long)(next(32)) << 32) + next(32);
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;
}
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);
}
然后那串数字生成的源头:
return ((long)(next(32)) << 32) + next(32);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询