技术中心

这里象征着我们的态度和能力

用java线程实现批量修改文件名
作者:中国IT实验室    来源:中国IT实验室    发布时间:2012-09-15      浏览次数:11459
分享到:
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
    /**
   
    * this program TODO
   
    * @version 1.0 2012-9-1
   
    * @ausor widjan wu
   
    */
   
    package file;
   
    import java.io.File;
   
    import java.util.ArrayList;
   
    import java.util.Scanner;
   
    import java.util.concurrent.*;
   
    public class ChangeFileName
   
    {
   
    public static void main(String[] args)
   
    {
   
    Scanner in = new Scanner(System.in);
   
    System.out.print(“Enter base directory :”);
   
    String directory = in.nextLine();
   
    System.out.print(“Enter key words:”);
   
    String keywords = in.nextLine();
   
    ExecutorService pool = Executors.newCachedThreadPool();
   
    ChangeName change = new ChangeName(new File(directory), keywords, pool);
   
    Future result = pool.submit(change);
   
    try {
   
    System.out.println(result.get() + “files were changed”);
   
    } catch (ExecutionException e) {
   
    e.printStackTrace();
   
    } catch (InterruptedException e) {
   
    }
   
    pool.shutdown();
   
    int largestPoolSize = ((ThreadPoolExecutor) pool)。getLargestPoolSize();
   
    System.out.println(“largest pool size :” + largestPoolSize);
   
    }
   
    }
   
    class ChangeName implements Callable
   
    {
   
    public ChangeName(File directory, String keywords, ExecutorService pool) {
   
    this.directory = directory;
   
    this.pool = pool;
   
    this.keywords = keywords;
   
    }
   
    public Integer call()
   
    {
   
    count = 0;
   
    try
   
    {
   
    File[] files = directory.listFiles();
   
    ArrayList   
    for (File file : files) {
   
    if (file.isDirectory()) {
   
    ChangeName change = new ChangeName(file, keywords, pool);
   
    Future result = pool.submit(change);
   
    } else {
   
    count++;
   
    String path = file.getPath();
   
    int index1 = path.lastIndexOf(“”);
   
    path = path.substring(0, index1+1);
   
    System.out.println(path);
   
    String oldName = file.getName();
   
    String fileType = oldName.substring(oldName.lastIndexOf(“.”));
   
    String newFName = path + keywords + count + fileType;
   
    file.renameTo(new File(newFName));
   
    }
   
    }
   
    for(Future result:results)
   
    {
   
    try
   
    {
   
    count +=result.get();
   
    }catch(ExecutionException e)
   
    {
   
    e.printStackTrace();
   
    }
   
    }
   
    }catch(InterruptedException e)
   
    {
   
    }
   
    return count;
   
    }
   
    private File directory;
   
    private String keywords;
   
    private ExecutorService pool;
   
    private int count;
   
    }
4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730