//1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } //2.创建文件 //import java.io.*; File myFilePath = new File(str1); try { if (!myFilePath.exists()) { myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println(str2); resultFile.close(); } catch (Exception e) { System.out.println("新建文件操作出错"); e.printStackTrace(); } //3.删除文件 //import java.io.*; File myDelFile = new File(str1); try { myDelFile.delete(); } catch (Exception e) { System.out.println("删除文件操作出错"); e.printStackTrace(); } //4.删除文件夹 //import java.io.*; File delFolderPath = new File(str1); try { delFolderPath.delete(); //删除空文件夹 } catch (Exception e) { System.out.println("删除文件夹操作出错"); e.printStackTrace(); } //5.删除一个文件下夹所有的文件夹 //import java.io.*; File delfile=new File(str1); File[] files=delfile.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ files[i].delete(); }
原文:https://www.cnblogs.com/miutic/p/15207606.html