
1、判断文件是否存在,不存在创建文件
File file=new File("C:\Users\QPING\Desktop\JavaScript\2.htm");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}2、判断文件夹是否存在,不存在创建文件夹
File file =new File("C:\Users\QPING\Desktop\JavaScript");
//如果文件夹不存在则创建
if (!file .exists() && !file .isDirectory())
{
System.out.println("//不存在");
file .mkdir();
} else
{
System.out.println("//目录存在");
}备注:文件路径是filePath = D:word1 est.txt 带文件的,使用getParentFile 获取父文件
if(!file.getParentFile().isDirectory()){
file.getParentFile().mkdirs();
}推荐教程:java快速入门
以上就是java中判断文件或文件夹是否存在的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号