Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109088
  • 博文数量: 9
  • 博客积分: 246
  • 博客等级: 二等列兵
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-30 15:29
文章分类

全部博文(9)

文章存档

2013年(1)

2012年(8)

我的朋友

分类: Java

2012-09-02 14:51:53


 

  1. /**
  2.  * this program TODO
  3.  * @version 1.1 2012-9-1
  4.  * @ausor widjan wu
  5.  */
  6. package file;

  7. import java.io.File;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.Scanner;
  13. import java.util.Set;
  14. import java.util.concurrent.*;

  15. public class ChangeFileName
  16. {

  17.     public static void main(String[] args)
  18.     {
  19.         Scanner in = new Scanner(System.in);
  20.         System.out.print("Enter base directory :");
  21.         String directory = in.nextLine();
  22.         System.out.print("Enter key words:");
  23.         String keywords = in.nextLine();

  24.         ExecutorService pool = Executors.newCachedThreadPool();
  25.         ChangeName change = new ChangeName(new File(directory), keywords, pool);

  26.         Future<Integer> result = pool.submit(change);

  27.         try {
  28.             System.out.println(result.get() + "files were changed");
  29.         } catch (ExecutionException e) {
  30.             e.printStackTrace();
  31.         } catch (InterruptedException e) {

  32.         }

  33.         pool.shutdown();

  34.         int largestPoolSize = ((ThreadPoolExecutor) pool).getLargestPoolSize();
  35.         System.out.println("largest pool size :" + largestPoolSize);
  36.     }

  37. }

  38. class ChangeName implements Callable<Integer>
  39. {
  40.     public ChangeName(File directory, String keywords, ExecutorService pool) {
  41.         this.directory = directory;
  42.         this.pool = pool;
  43.         this.keywords = keywords;
  44.     }

  45.     public Integer call()
  46.     {
  47.         count = 0;

  48.         try
  49.         {
  50.             File[] files = directory.listFiles();
  51.             ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
  52.             
  53.             HashMap failsMap = new HashMap();
  54.             
  55.             
  56.             for (File file : files) {
  57.                 if (file.isDirectory()) {
  58.                     ChangeName change = new ChangeName(file, keywords, pool);
  59.                     Future<Integer> result = pool.submit(change);
  60.                 } else {
  61.                     count++;
  62.                     String path = file.getPath();
  63.                     int index1 = path.lastIndexOf("\\");
  64.                     path = path.substring(0, index1+1);    
  65.                     
  66.                     String oldName = file.getName();    
  67.                     
  68.                     String fileType = oldName.substring(oldName.lastIndexOf("."));
  69.                     String newFName = path + keywords + count + fileType;
  70.                     boolean success = file.renameTo(new File(newFName));    
  71.                     if (!success)
  72.                     {
  73.                         count--;
  74.                         failsMap.put(file, newFName);                        
  75.                     }
  76.                 }
  77.             }
  78.             
  79.             if(!failsMap.isEmpty())
  80.             {
  81.                 Set <File> c = failsMap.keySet();                
  82.                 String name = "";                
  83.                 
  84.                 for(File file: c)
  85.                 {
  86.                     name = (String) failsMap.get(file);
  87.                     file.renameTo(new File(name));                    
  88.                 }
  89.             }
  90.             
  91.             for(Future<Integer> result:results)
  92.             {
  93.                 try
  94.                 {
  95.                     count +=result.get();
  96.                 }catch(ExecutionException e)
  97.                 {
  98.                     e.printStackTrace();
  99.                 }
  100.             }
  101.         
  102.         }catch(InterruptedException e)
  103.         {
  104.             
  105.         }

  106.         return count;
  107.     }

  108.     private File directory;
  109.     private String keywords;
  110.     private ExecutorService pool;
  111.     private int count;
  112. }


 

阅读(1365) | 评论(0) | 转发(0) |
0

上一篇:java 线程同步

下一篇:java访问权限

给主人留下些什么吧!~~