利用Java实现指定文件夹下的照片以自定义格式移动

  • 2020 年 3 月 12 日
  • 筆記

前几天本猿的大学同学,一个漂亮的小姐姐工作时遇到了一个问题,她的需求是,在公司局域网的电脑上下载大量的图片重命名成指定得1、2、3…..以此类推,需要当天完成,我就临时给写了一个小demo。

我的想法是采用linux的原理不就好实现吗,直接mv到指定文件夹下再给一个新的名字不就实现了吗

我给出的代码如下(不是很成熟,还请大佬们多多指教):

 1 public class FileRename {   2     public static void main(String[] args) throws Exception {   3         Integer n = Integer.valueOf(args[0]);   4         String src = args[1];   5         String dest = args[2];   6         for (int i = 1; i <= n; i++){   7             String name2 =" ("+i+")"+".png";   8             String newName = src +name2;   9             System.out.println("success:"+newName);  10             File afile=new File(newName);  11             FileInputStream fis = new FileInputStream(afile);  12             FileOutputStream fos = new FileOutputStream(new File(dest+i+".png"));  13             byte[] read = new byte[1024];  14             int len = 0;  15             while((len = fis.read(read))!= -1){  16                 fos.write(read,0,len);  17             }  18             fis.close();  19             fos.flush();  20             fos.close();  21  22         }  23  24     }  25  26 }

因为她没有IDE所以参数都改成传参
args[0]是需要改名的文件数
args[1]是原路径
args[2]是目标路径
剩下的就javac编译 java + 参数运行就可以了
此方式简陋但是也还算实用

重命名的方法也试过了但是不符合要求所以出此下策