java IO 文件批量重命名
java IO 文件批量重命名
package com.vfsd.renamefile; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /******************************************************************************************************* * Copyright: vc1.0 2018. All rights reserved. <br> * The whole package including this class is licensed under <br> * <br> * @ClassName: <br> * @Directory: <br> * @author: lzb <br> * @version: v1.0.0 <br> * @date: <br> * * @Statement: If you are using the package or parts of it in any commercial way, a commercial license is required. <br> * Visit <a href='http://www.bim-times.com'>http://www.bim-times.com</a> for more information.<br> * *********************************************************************************************************/ public class RenameFile { public static void main(String[] args) throws Exception { /* String fileDirStr = "D:\\�ο��ֲ�\\���\\"; renameFile(fileDirStr); */ //mergeFile(); String fileDir = "D:\\zy_img_20190916\\1\\"; String fileDir2 = "D:\\zy_img_20190916\\2\\"; renameFile2(fileDir,fileDir2);//图片拷贝并重命名 //renameFile3(fileDir,fileDir2);//json拷贝并重命名 } public static void renameFile(String fileDirStr) throws Exception{ File fileDir = new File(fileDirStr); if(fileDir.isDirectory()) { File[] filesList = fileDir.listFiles(); for(int i=0;i<filesList.length;i++) { File fileIndex = filesList[i]; if(fileIndex.getName().indexOf(".jpg")>0 ||fileIndex.getName().indexOf(".png")>0 ||fileIndex.getName().indexOf(".jpeg")>0||fileIndex.getName().indexOf(".gif")>0) { //String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length())); fileIndex.renameTo(fileNew); System.out.println(fileNew.getName()); } } } } /** * 图片文件拷贝并重命名 * @param fileDirStr * @param fileDir2 * @throws Exception */ public static void renameFile2(String fileDirStr,String fileDir2) throws Exception{ File fileDir = new File(fileDirStr); if(fileDir.isDirectory()) { File[] filesList = fileDir.listFiles(); for(int i=0;i<filesList.length;i++) { File fileIndex = filesList[i]; if(fileIndex.getName().indexOf(".jpg")>0 ||fileIndex.getName().indexOf(".png")>0 ||fileIndex.getName().indexOf(".jpeg")>0||fileIndex.getName().indexOf(".JPG")>0) { //String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDir2+(i+1)+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length())); fileIndex.renameTo(fileNew); System.out.println(fileNew.getName()); } } } } /** * JSON文件拷贝并重命名 * @param fileDirStr * @param fileDir2 * @throws Exception */ public static void renameFile3(String fileDirStr,String fileDir2) throws Exception{ File fileDir = new File(fileDirStr); if(fileDir.isDirectory()) { File[] filesList = fileDir.listFiles(); for(int i=0;i<filesList.length;i++) { File fileIndex = filesList[i]; if(fileIndex.getName().indexOf(".json")>0) { //String newName=fileDirStr+i+fileIndex.getName().substring(fileIndex.getName().length()-4, fileIndex.getName().length()); File fileNew = new File(fileDir2+(i+1)+fileIndex.getName().substring(fileIndex.getName().length()-5, fileIndex.getName().length())); fileIndex.renameTo(fileNew); System.out.println(fileIndex.getName()+" "+fileNew.getName()); } } } } /** * �Ѷ���ļ����е�ͼƬ�ϲ� * @throws IOException */ public static void mergeFile() throws IOException { //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\�_�����ע\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder301-350_labeled\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder451-600\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder601-750\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder150-300_labeled\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\maskedhumanandladder901-1050\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder751-900\\mask_data\\"; //String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder1201-1350\\mask_data\\"; String dir1 = "D:\\�ο��ֲ�\\����\\��ע-2019\\humanandladder1051-1200\\mask_data\\"; File fileDir = new File(dir1); File fileDirs[] = fileDir.listFiles(); for(int i=0;i<fileDirs.length;i++) { File fileIndexDir = fileDirs[i]; if(fileIndexDir.isDirectory()) { File fileLists[] = fileIndexDir.listFiles(); File fileMaskImg = new File(fileIndexDir.getPath()+"\\label.png"); if(fileMaskImg.exists()) { System.out.println(fileIndexDir.getName().replace("_json", "")); File newFile = new File("D:\\�ο��ֲ�\\����\\��ע-2019\\train_data_20190227\\cv2_mask\\"+fileIndexDir.getName().replace("_json", "")+".png"); copyFile(fileMaskImg,newFile); } } } } /** * �����ļ� * @param oldFile * @param newFile * @throws IOException */ public static void copyFile(File oldFile,File newFile) throws IOException{ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(oldFile); fileOutputStream = new FileOutputStream(newFile); byte[] bytes = new byte[1024]; int index=0; while((index=fileInputStream.read(bytes))!=-1) { fileOutputStream.write(bytes, 0, index); } }catch(Exception e) { e.printStackTrace(); }finally { fileOutputStream.close(); fileInputStream.close(); } } }
QQ 3087438119