62,614
社区成员
发帖
与我相关
我的任务
分享
if(file.createNewFile()){
System.out.println("创建成功。。。");
}else{
System.out.println("创建失败。。。");
}
如果存在。。。直接走else了。。。不会进入上面的代码,所以无论文件是否存在都不会执行System.out.println("创建失败。。。");
public static void main(String[] args) {
createFile("E:/test/test.txt");//test文件夹没有写入权限。。。
}
public static void createFile(String s){
File file=new File(s);
if(!file.exists()){
boolean bool=false;
try {
bool = file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bool){
System.out.println("创建成功。。。");
}else{
System.out.println("创建失败。。。");
}
}else{
System.out.println("已经存在。。。");
}
}