用java线程实现批量修改文件名

用java线程实现批量修改文件名,第1张

;   /        this program TODO        @version         @ausor widjan wu        /        package file;        import java io File;        import java util ArrayList;        import java util Scanner;        import ncurrent ;        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<Integer> 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<Integer>        {        public ChangeName(File directory String keywords ExecutorService pool) {        this directory = directory;        this pool = pool;        this keywords = keywords;        }        public Integer call()        {        count = ;        try        {        File[] files = directory listFiles()         ArrayList<Future<Integer》 results = new ArrayList<Future<Integer》()         for (File file : files) {        if (file isDirectory()) {        ChangeName change = new ChangeName(file keywords pool)         Future<Integer> result = pool submit(change)         } else {        count++;        String path = file getPath()         int index = path lastIndexOf( \\ )         path = path substring( index + )         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<Integer> 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;        } lishixinzhi/Article/program/Java/gj/201311/27511

以上就是关于用java线程实现批量修改文件名全部的内容,包括:用java线程实现批量修改文件名、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9877542.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存