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

Python016

用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批量修改文件名:

public static void main(String[] args) {

updateFileNames("D:\\jjjj", "第")

}

public static void updateFileNames(String url, String index){

File file = new File(url)

//判断文件目录是否存在,且是文件目录,非文件

if(file.exists() &&file.isDirectory()){

File[] childFiles = file.listFiles()

String path = file.getAbsolutePath()

for(File childFile : childFiles){

//如果是文件

if(childFile.isFile()){

String oldName = childFile.getName()

String newName = oldName.substring(oldName.indexOf(index))

childFile.renameTo(new File(path + "\\" + newName))

}

}

}

}

/*

怎么用java同时实现批量删除,批量修改?

*/

//1,可以利用循环批量来操作数组元素

int arr[] = new int[100]//定义一个数组,长度为100

//对该数组进行批量赋值

for (int i = 0i <arr.lengthi++) {

arr[i] = i

}

//2,对于集合,可以使用removeALL方法进行批量删除

List<String>list = new ArrayList<String>()

list.add("1")

list.add("2")

list.add("3")

list.add("4")

list.add("5")

list.removeAll(list)

System.out.println(list)

//这上是java自带的一些方法

//3,JDBC

/**

* 对于数据库的操作,就需要用SQL语言来批量处理了;

* 比如:select *from EMP

*

* 利用JDBC的一些方法,比如预处理命令,可以对数据库进行批量操作,

*/