知识大全 用java线程实现批量修改文件名
Posted 知
篇首语:注意力是智慧的门户。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 用java线程实现批量修改文件名相关的知识,希望对你有一定的参考价值。
/** * 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; cha138/Article/program/Java/gj/201311/27511相关参考