先创建一级根目录
File rootfolder = new File("C:\\Photodistinguish\\"); // 如果文件夹不存在则创建 if (!rootfolder.exists() && !rootfolder.isDirectory()) { rootfolder.mkdir(); System.out.println("c盘根目录不存在"); } else { System.out.println("c盘根目录存在"); }
在创建子目录,然后创建文件
File inputfolder = new File("C:\\Photodistinguish\\identifiedphotos\\"); // 如果文件夹不存在则创建 if (!inputfolder.exists() && !inputfolder.isDirectory()) { inputfolder.mkdir(); System.out.println("//txt目录不存在"); } else { System.out.println("//txt目录存在"); } String filepath = "C:\\Photodistinguish\\identifiedphotos\\text.txt"; File txtfile = new File(filepath); // 如果文件不存在则创建 if (!txtfile.exists()) { try { txtfile.createNewFile(); System.out.println("//txt目录下文件不存在"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }