Java创建、重命名、删除文件和文件夹

  1. package my.file;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.OutputStreamWriter;  
  5. import java.io.Writer;  
  6.   
  7. public class TravelAllFile {  
  8.  public static void main(String[] args) {  
  9.   String path = "E:/文件夹";  
  10.   File f = new File(path);  
  11.   if (f.isDirectory()) {  
  12.    new TravelAllFile().getFileName(f);  
  13.   }  
  14.     
  15.   delFolder("E:/文件夹");  
  16.   System.out.println("deleted");  
  17.  }  
  18.    
  19.  // 递归查找函数,参数为file对象  
  20.  public void getFileName(File f) {  
  21.   // File 数组  
  22.   File[] files = f.listFiles();  
  23.   
  24.   for (int i = 0; i < files.length; i++) {  
  25.    // 递归出子目录  
  26.    if (files[i].isDirectory()) {  
  27.     System.out.println("子目录是:" + files[i].getName());  
  28.     getFileName(files[i]);  
  29.     // 递归出子文件  
  30.    } else {  
  31.     System.out.println(files[i].getName());  
  32.    }  
  33.   
  34.   }  
  35.  }  
  36.   
  37.  //重命名  
  38.  public void rename(){  
  39.   File fl=new File("E://文件夹");  // 这里写上发替换的文件夹路径,注意使用双斜杠  
  40.     String[] files=fl.list();  
  41.     File f=null;  
  42.     String filename="";  
  43.     for(String file:files)  
  44.     {  
  45.      f=new File(fl,file);// 注意,这里一定要写成File(fl,file)如果写成File(file)是行不通的,一定要全路径  
  46.      filename=f.getName();  
  47.      // System.out.println(filename);  
  48.      f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace("要替换掉的内容""替换成的内容")));// 这里可以反复使用replace替换,当然也可以使用正则表达式来替换了  
  49.        
  50.     }  
  51.  }  
  52.    
  53.  //删除文件夹  
  54.  //param folderPath 文件夹完整绝对路径  
  55.   
  56.  public static void delFolder(String folderPath) {  
  57.   try {  
  58.    delAllFile(folderPath); //删除完里面所有内容  
  59.    String filePath = folderPath;  
  60.    filePath = filePath.toString();  
  61.    java.io.File myFilePath = new java.io.File(filePath);  
  62.    myFilePath.delete(); //删除空文件夹  
  63.   } catch (Exception e) {  
  64.    e.printStackTrace();  
  65.   }  
  66.  }  
  67.   
  68.  //删除指定文件夹下所有文件  
  69.  //param path 文件夹完整绝对路径  
  70.  public static boolean delAllFile(String path) {  
  71.   boolean flag = false;  
  72.   File file = new File(path);  
  73.   if (!file.exists()) {  
  74.    return flag;  
  75.   }  
  76.   if (!file.isDirectory()) {  
  77.    return flag;  
  78.   }  
  79.   String[] tempList = file.list();  
  80.   File temp = null;  
  81.   for (int i = 0; i < tempList.length; i++) {  
  82.    if (path.endsWith(File.separator)) {  
  83.     temp = new File(path + tempList[i]);  
  84.    } else {  
  85.     temp = new File(path + File.separator + tempList[i]);  
  86.    }  
  87.    if (temp.isFile()) {  
  88.     temp.delete();  
  89.    }  
  90.    if (temp.isDirectory()) {  
  91.     delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
  92.     delFolder(path + "/" + tempList[i]);//再删除空文件夹  
  93.     flag = true;  
  94.    }  
  95.   }  
  96.   return flag;  
  97.  }  
  98.    
  99.  /** 
  100.   * 创建目录 
  101.   * @param dirPath 
  102.   */  
  103.  static Boolean createDir(String dirPath){  
  104.   File dirFile = new File(dirPath);  
  105.   return dirFile.mkdirs();  
  106.  }  
  107.    
  108.  /** 
  109.   * 创建文件 
  110.   * @param filePath 
  111.   * @param content 
  112.   * @return Boolean 
  113.   */  
  114.  static Boolean createFile(String filePath,String content){  
  115.   Writer writer = null;  
  116.   File file = new File(filePath);  
  117.   File temp = new File(file.getParent());  
  118.   if(!temp.exists()){  
  119.    createDir(file.getParent());  
  120.   }  
  121.   try{  
  122.    writer = new OutputStreamWriter(new FileOutputStream(file));  
  123.    writer.write( content );  
  124.   }catch(Exception e){  
  125.    e.printStackTrace();  
  126.    return false;  
  127.   }finally{  
  128.    try{  
  129.     if(writer!=null){  
  130.      writer.close();  
  131.     }  
  132.    }catch(Exception e1){  
  133.     e1.printStackTrace();  
  134.    }  
  135.   }  
  136.   return true;  
  137.  }  
  138. }  

猜你喜欢

转载自blog.csdn.net/do_you_ac_today/article/details/80117454