怎么在java根目录下创建文件

2025-06-29 13:35:22
推荐回答(3个)
回答1:

	public static void main(String[] args) {
String dir = System.getProperty("user.dir");
dir = dir + File.separator + "arptmp";
File f = new File(dir);
if(!f.exists()) {
f.mkdirs();
}
dir = f.getPath() + File.separator + "servlet_.dot";
f = new File(dir);
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

回答2:

new file的时候写绝对路径。不要写相对路径,

回答3:

String rootPath=request.getRealPath("/");//获取项目根目录
String path=rootPath+"arptmp\\";//这就是你想要的目录