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

Python015

用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))

}

}

}

}

前台获取选中的id以,分割最后把数量也拼接上去传到后台

前台:

var count=$('#count').val()

var arrayrow = $('#ec_table').datagrid('getSelections')

var idstr=''

$(arrayrow).each(function(i,row){

idstr+=row.productId+','

})

var params='params='+idstr+count

$.post("updateProduc.action",params,function(a){

},'json')

后台:

//Integer.parseInt(str[str.length-1])是最后拼接的数量

String []str=params.split(",")

for (int i = 0i <str.length-1i++) {

int num=Integer.parseInt(str[i])

product=productService.getFous(num) //根据id查询商品

product.setCount(Integer.parseInt(str[str.length-1]))

productService.update(product)

}