- Java code
public static void main(String[] args) throws IOException { File dir = new File("c/exam/haha"); if (dir.exists() && dir.isDirectory()) { System.out.println("The directory already exits!".toUpperCase()); } else { if (dir.mkdirs()) { System.out.println("the directory has been created!".toUpperCase()); File f = new File(dir, "test.txt"); f.createNewFile(); f.setReadOnly(); System.out.println("end of creation".toUpperCase()); } else { System.out.println("failed!".toUpperCase()); } } }
第一次运行时,输出创建成功的信息
而且再次运行也会提示目录已经存在
但是通过实际查看一下,发现并没有生成目录和文件,为什么?
------解决方案--------------------
创建了,在你项目根目录下生成了c\exam\haha\test.txt
------解决方案--------------------
刷新下工程目录。