1 //1.创建文件夹 2 //import java.io.*; 3 File myFolderPath = new File(str1); 4 try { 5 if (!myFolderPath.exists()) { 6 myFolderPath.mkdir(); 7 } 8 } 9 catch (Exception e) { 10 System.out.println("新建目录操作出错"); 11 e.printStackTrace(); 12 } 13 14 //2.创建文件 15 //import java.io.*; 16 File myFilePath = new File(str1); 17 try { 18 if (!myFilePath.exists()) { 19 myFilePath.createNewFile(); 20 } 21 FileWriter resultFile = new FileWriter(myFilePath); 22 PrintWriter myFile = new PrintWriter(resultFile); 23 myFile.println(str2); 24 resultFile.close(); 25 } 26 catch (Exception e) { 27 System.out.println("新建文件操作出错"); 28 e.printStackTrace(); 29 } 30 31 //3.删除文件 32 //import java.io.*; 33 File myDelFile = new File(str1); 34 try { 35 myDelFile.delete(); 36 } 37 catch (Exception e) { 38 System.out.println("删除文件操作出错"); 39 e.printStackTrace(); 40 } 41 42 //4.删除文件夹 43 //import java.io.*; 44 File delFolderPath = new File(str1); 45 try { 46 delFolderPath.delete(); //删除空文件夹 47 } 48 catch (Exception e) { 49 System.out.println("删除文件夹操作出错"); 50 e.printStackTrace(); 51 } 52 53 //5.删除一个文件下夹所有的文件夹 54 //import java.io.*; 55 File delfile=new File(str1); 56 File[] files=delfile.listFiles(); 57 for(int i=0;ifolderList = new LinkedList (); 153 folderList.add(str1); 154 while (folderList.size() > 0) { 155 File file = new File(folderList.peek()); 156 folderList.removeLast(); 157 File[] files = file.listFiles(); 158 ArrayList fileList = new ArrayList (); 159 for (int i = 0; i < files.length; i++) { 160 if (files[i].isDirectory()) { 161 folderList.add(files[i].getPath()); 162 } else { 163 fileList.add(files[i]); 164 } 165 } 166 for (File f : fileList) { 167 str2=f.getAbsoluteFile(); 168 str3 169 } 170 } 171 172 //13.复制文件夹 173 //import java.io.*; 174 //import java.util.*; 175 LinkedList folderList = new LinkedList (); 176 folderList.add(str1); 177 LinkedList folderList2 = new LinkedList (); 178 folderList2.add(str2+ str1.substring(str1.lastIndexOf("\\"))); 179 while (folderList.size() > 0) { 180 (new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹 181 File folders = new File(folderList.peek()); 182 String[] file = folders.list(); 183 File temp = null; 184 try { 185 for (int i = 0; i < file.length; i++) { 186 if (folderList.peek().endsWith(File.separator)) { 187 temp = new File(folderList.peek() + File.separator 188 + file[i]); 189 } else { 190 temp = new File(folderList.peek() + File.separator + file[i]); 191 } 192 if (temp.isFile()) { 193 FileInputStream input = new FileInputStream(temp); 194 FileOutputStream output = new FileOutputStream( 195 folderList2.peek() + File.separator + (temp.getName()).toString()); 196 byte[] b = new byte[5120]; 197 int len; 198 while ((len = input.read(b)) != -1) { 199 output.write(b, 0, len); 200 } 201 output.flush(); 202 output.close(); 203 input.close(); 204 } 205 if (temp.isDirectory()) { // 如果是子文件夹 206 for (File f : temp.listFiles()) { 207 if (f.isDirectory()) { 208 folderList.add(f.getPath()); 209 folderList2.add(folderList2.peek() 210 + File.separator + f.getName()); 211 } 212 } 213 } 214 } 215 } catch (Exception e) { 216 //System.out.println("复制整个文件夹内容操作出错"); 217 e.printStackTrace(); 218 } 219 folderList.removeFirst(); 220 folderList2.removeFirst(); 221 } 222 223 //14.复制一个文件夹下所有的文件夹到另一个文件夹下 224 //import java.io.*; 225 //import java.util.*; 226 File copyfolders=new File(str1); 227 File[] copyfoldersList=copyfolders.listFiles(); 228 for(int k=0;k folderList=new ArrayList (); 231 folderList.add(copyfoldersList[k].getPath()); 232 ArrayList folderList2=new ArrayList (); 233 folderList2.add(str2+"/"+copyfoldersList[k].getName()); 234 for(int j=0;j folderList = new LinkedList (); 277 folderList.add(str1); 278 LinkedList folderList2 = new LinkedList (); 279 folderList2.add(str2 + str1.substring(str1.lastIndexOf("\\"))); 280 while (folderList.size() > 0) { 281 (new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹 282 File folders = new File(folderList.peek()); 283 String[] file = folders.list(); 284 File temp = null; 285 try { 286 for (int i = 0; i < file.length; i++) { 287 if (folderList.peek().endsWith(File.separator)) { 288 temp = new File(folderList.peek() + File.separator + file[i]); 289 } else { 290 temp = new File(folderList.peek() + File.separator + file[i]); 291 } 292 if (temp.isFile()) { 293 FileInputStream input = new FileInputStream(temp); 294 FileOutputStream output = new FileOutputStream( 295 folderList2.peek() + File.separator + (temp.getName()).toString()); 296 byte[] b = new byte[5120]; 297 int len; 298 while ((len = input.read(b)) != -1) { 299 output.write(b, 0, len); 300 } 301 output.flush(); 302 output.close(); 303 input.close(); 304 if (!temp.delete()) 305 System.out.println("删除单个文件操作出错!"); 306 } 307 if (temp.isDirectory()) { // 如果是子文件夹 308 for (File f : temp.listFiles()) { 309 if (f.isDirectory()) { 310 folderList.add(f.getPath()); 311 folderList2.add(folderList2.peek() + File.separator + f.getName()); 312 } 313 } 314 } 315 } 316 } catch (Exception e) { 317 // System.out.println("复制整个文件夹内容操作出错"); 318 e.printStackTrace(); 319 } 320 folderList.removeFirst(); 321 folderList2.removeFirst(); 322 } 323 File f = new File(str1); 324 if (!f.delete()) { 325 for (File file : f.listFiles()) { 326 if (file.list().length == 0) { 327 System.out.println(file.getPath()); 328 file.delete(); 329 } 330 } 331 } 332 //16.移动一个文件夹下所有的文件夹到另一个目录下 333 //import java.io.*; 334 //import java.util.*; 335 File movefolders=new File(str1); 336 File[] movefoldersList=movefolders.listFiles(); 337 for(int k=0;k folderList=new ArrayList (); 340 folderList.add(movefoldersList[k].getPath()); 341 ArrayList folderList2=new ArrayList (); 342 folderList2.add(str2+"/"+movefoldersList[k].getName()); 343 for(int j=0;j folderList=new ArrayList (); 390 folderList.add(str1); 391 ArrayList folderList2=new ArrayList (); 392 folderList2.add(str2); 393 for(int j=0;j