Java——FIle:

1.读取文件夹中的文件

/**
     * 读取图片文件
     * @param imgPath 文件所在的文件夹绝对路径
     * @return
     * @throws FileNotFoundException
     */
    public static List<String> readImg(String imgPath)throws FileNotFoundException{
        System.out.println("----------------------------读取start--------------------------");
        List<String> abPathList = new ArrayList<String>();//建立绝对路径集合
        List<String> nameList = new ArrayList<String>();//建立相对路径集合
        try {
            File file = new File(imgPath);
            //判断不是目录
            if (!file.isDirectory()) {
                    System.out.println("文件");
                    System.out.println("path=" + file.getPath());
                    System.out.println("absolutepath=" + file.getAbsolutePath());
                    System.out.println("name=" + file.getName());
            }
             //判断是目录
             else if (file.isDirectory()) {
                    System.out.println("文件夹");
                    String[] filelist = file.list();
                    for (int i = 0; i < filelist.length; i++) {
                            File readfile = new File(imgPath + "\\" + filelist[i]);
                            if (!readfile.isDirectory()) {
                                    System.out.println("path=" + readfile.getPath());
                                    System.out.println("parent=" + readfile.getParent());
                                    System.out.println("绝对路径=" + readfile.getAbsolutePath());
                                    System.out.println("名称List:"+ readfile.getName());
                     abPathList.add(readfile.getAbsolutePath());//存储绝对路径
                                    nameList.add(readfile.getName());//存储名称
                            } else if (readfile.isDirectory()) {
                                    readImg(imgPath + "\\" + filelist[i]);
                            }
                    }
            }
    } catch (FileNotFoundException e) {
            System.out.println("readfile() Exception:" + e.getMessage());
    }
        System.out.println("----------------------------读取end--------------------------");
        return nameList;
    }
        

2.复制文件夹及内容到另一个文件夹

/** 
     * 复制整个文件夹内容 
     * @param oldPath String 原文件路径 如:F:/taobaoCsv/all
     * @param newPath String 复制后路径 如: F:/taobaoCsv/allnew
     * @return boolean 
     */ 
   public static void copyFolder(String oldPath, String newPath) { 
       System.out.println("----------------------------复制start--------------------------");
       System.out.println("正在复制...");
       try { 
           (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
           File a=new File(oldPath); 
           String[] file=a.list(); 
           File temp=null; 
           for (int i = 0; i < file.length; i++) { 
               if(oldPath.endsWith(File.separator)){ 
                   temp=new File(oldPath+file[i]); 
               } 
               else{ 
                   temp=new File(oldPath+File.separator+file[i]); 
               } 
               if(temp.isFile()){ 
                   FileInputStream input = new FileInputStream(temp); 
                   FileOutputStream output = new FileOutputStream(newPath + "/" + 
                           (temp.getName()).toString()); 
                   byte[] b = new byte[1024 * 5]; 
                   int len; 
                   while ( (len = input.read(b)) != -1) { 
                       output.write(b, 0, len); 
                   } 
                   output.flush(); 
                   output.close(); 
                   input.close(); 
               } 
               if(temp.isDirectory()){//如果是子文件夹 
                   copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
                   System.out.println("子文件夹...");
               } 
           } 
           System.out.println("复制成功。");
           System.out.println("----------------------------复制end--------------------------");
       } 
       catch (Exception e) { 
           System.out.println("复制整个文件夹内容操作出错"); 
           e.printStackTrace(); 
       } 
   }

3.批量修改文件名

/**
    * //批量修改文件名(文件名后面+‘ .jpg’)
    * @param folderPath 文件夹路径
    */
   public static void editName(String folderPath){
     //判断不是文件夹...提示错误
       System.out.println("----------------------------修改start--------------------------");
     //获取文件夹中的所有文件
     File folder = new File(folderPath);
     File[] fileList = folder.listFiles();
     //批量修改
     for (int i = 0; i < fileList.length; i++) {
         File oldFile = fileList[i];
         System.out.println("修改前文件名称是:"+oldFile.getName());
         
         //修改文件名称:rootPath/filename .jpg
         File newFile = new File(oldFile.getParent()+File.separator+oldFile.getName()+" .jpg");
         
         System.out.println("修改后文件名称是:"+newFile.getName());
         
         if (oldFile.renameTo(newFile)){
              System.out.println("修改成功!");
          }else{
              System.out.println("修改失败");
          }
     }
     System.out.println("----------------------------修改end--------------------------");
   }

4.删除

  1).删除文件夹

  2).删除文件夹的内容,但不删除文件夹

/**
 * 删除文件夹
 * @param folderPath 文件夹完整绝对路径
 */
public static void delFolder(String folderPath) {
   try {
    delAllFile(folderPath); // 删除完里面所有内容
    String filePath = folderPath;
    filePath = filePath.toString();
    java.io.File myFilePath = new java.io.File(filePath);
    myFilePath.delete(); // 删除空文件夹
   } catch (Exception e) {
    e.printStackTrace();
   }
}

/**
 * 删除指定文件夹下所有文件
 * @param path 文件夹完整绝对路径
 * @return
 */
public static boolean delAllFile(String path) {
   boolean flag = false;
   File file = new File(path);
   if (!file.exists()) {
    return flag;
   }
   if (!file.isDirectory()) {
    return flag;
   }
   String[] tempList = file.list();
   File temp = null;
   for (int i = 0; i < tempList.length; i++) {
    if (path.endsWith(File.separator)) {
     temp = new File(path + tempList[i]);
    } else {
     temp = new File(path + File.separator + tempList[i]);
    }
    if (temp.isFile()) {
     temp.delete();
    }
    if (temp.isDirectory()) {
     delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
     delFolder(path + "/" + tempList[i]);// 再删除空文件夹
     flag = true;
    }
   }
   return flag;
}

 

 

posted @ 2014-04-10 11:36  艺言弈行  阅读(340)  评论(0编辑  收藏  举报