version 1.2
1.修改不用輸入擴展名
2.輸出路徑可選。默認會在輸入路徑下建文件夾
前沿:
現在開發中ios,android會使用一套圖,但是ui設計師給的圖命名是以@1x,@2x,@3x這樣命名的,android 客戶端使用起來就略嫌麻煩了,這個小工具可以實現簡單的分包。
原理:
I/o流讀取 testPicture中的@1x,@2x,@3x 文件進行整理,按序輸出舊文件文字同時,需要輸入一個新文件名 + 后綴名,然后動態進行分組到desPicture中的文件夾1,文件夾2,文件夾3。1,2,3文件夾分別對應@1x,@2x,@3x.
Eg:盡量配對出現 @1x,@2x,@3x.
1.首先需要兩個文件夾,一個里面存放着待處理的文件,另一個存放處理后的文件
testPicture
desPicture
步驟:
1.進入dos窗口
Window+r —–>cmd
2.進入mutiFile.jar 根目錄 輸入
java -jar MutiFile-x.x.x.jar srcFile outputDir
其中:MutiFile-x.x.x.jar ,是工具類
srcFile 為待處理文件夾路徑
outputDir 為處理后的文件輸出路徑
處理后的文件,已經進行分組成 @1x,@2x,@3x
貼出來源碼:
package com.nuoyuan.mutiFile; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; /** * version 1.2 * 1. 增加支持 不寫后綴名字 * 2. 增加輸入輸出路徑可選 * @author weichyang * */ public class MutiFile { private static final String USAGE_TEXT = "Usage: java -jar MutiFile-x.x.x.jar srcFile outputDir "; private static Map<String, String> onePlus = new HashMap(); private static Map<String, String> twoPlus = new HashMap(); private static Map<String, String> threePlus = new HashMap(); private static List newFilePath = new ArrayList<String>(); private static String[] spiltName = { "@1x", "@2x", "@3x" }; private static ArrayList<String> filelist = new ArrayList<String>(); private static String tempFileName = "xx@xx"; private static String beforeName = "&6%"; public static void main(String[] args) throws IOException { if (args.length < 1) { println(USAGE_TEXT); return; } String resPathString = ""; String desPathString = ""; if (args.length == 1) { resPathString = args[0]; desPathString = args[0]; } else { resPathString = args[0]; desPathString = args[1]; } File resFile = new File(resPathString); File desFile = new File(desPathString); if (!resFile.exists()) { println(" file '" + desPathString + "' is not exists or not readable."); println(USAGE_TEXT); System.exit(1); } if (!desFile.exists()) { // desfile.createNewFile(); println(" file '" + desFile.getAbsolutePath() + "' is not exists or not readable."); println(USAGE_TEXT); System.exit(1); return; } else { for (int i = 1; i <= 3; i++) { String newPathString = desPathString + "\\" + i; new File(newPathString).mkdirs(); newFilePath.add(newPathString + "\\"); } } // 打印資源文件 println("res File: " + resFile.getAbsolutePath()); println("output File: " + desFile.getAbsolutePath()); // 資源存在是否為null getFiles(resFile); } /* * 通過遞歸得到某一路徑下所有的目錄及其文件 */ public static void getFiles(File filePath) throws FileNotFoundException, IOException { File[] files = filePath.listFiles(); if (files.length == 0) { println(filePath + " is have't file exit!!"); System.exit(1); return; } for (File file : files) { if (file.isDirectory()) { /* * 遞歸調用 */ getFiles(file); filelist.add(file.getAbsolutePath()); System.out.println("show " + filePath + "下所有子目錄及其文件" + file.getAbsolutePath()); } else { String currentFilePath = file.getAbsolutePath().toString(); String fileName = splitFileName(currentFilePath); System.out.println("current file name is:" + fileName); if (!fileName.contains("@")) { continue; } if (fileName.contains(spiltName[0])) { onePlus.put(fileName, currentFilePath); String fileNameString = InputCode(fileName); fromToAnotherDes(currentFilePath, newFilePath.get(0) .toString() + fileNameString); } else if (fileName.contains(spiltName[1])) { twoPlus.put(fileName, newFilePath.get(1).toString() + fileName); String fileNameString = InputCode(fileName); fromToAnotherDes(currentFilePath, newFilePath.get(1) .toString() + fileNameString); } else if (fileName.contains(spiltName[2])) { threePlus.put(fileName, newFilePath.get(2).toString() + fileName); String fileNameString = InputCode(fileName); fromToAnotherDes(currentFilePath, newFilePath.get(2) .toString() + fileNameString); } } } System.out.print("/****** File rename Success *********/\n"); System.out.print("共修改@1x 文件 " + onePlus.size() + "個\n"); System.out.print("共修改@2x 文件 " + twoPlus.size() + "個\n"); System.out.print("共修改@3x 文件 " + threePlus.size() + "個\n"); System.out.print("總共修文件 " + threePlus.size() + onePlus.size() + twoPlus.size() + "個\n"); System.out.print("/**************************************/\n"); } /** * 分割文件名字 * * @param str * @return */ public static String splitFileName(String str) { String[] strs = str.split("\\\\"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < strs.length; i++) { if (i == strs.length - 1) sb.append(strs[i]); } return sb.toString(); } /** * 拷貝一個文件到另一個問價中 * * @param srcFile * @param desFile * @throws FileNotFoundException * @throws IOException */ public static void fromToAnotherDes(String srcFile, String desFile) { try { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(desFile); int len = 0; byte[] buf = new byte[1024]; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 接受輸入的數字進行設置文件名字 一次輸入使用三次 * * @return */ public static String InputCode(String fileName) { String extNameString = getExtension(fileName); String realNameString = fileName.split("@")[0]; if (realNameString.equals(beforeName)) { return tempFileName + extNameString; } Scanner scanner = new Scanner(System.in);// 創建輸入流掃描器 System.out.print("please input file name:\n");// 提示用戶輸入 // 獲取用戶輸入的一行文本 String line = scanner.nextLine(); // 打印對輸入文本的描述 tempFileName = line; beforeName = realNameString; return line + extNameString; } public static void println(String msg) { System.out.println(msg); } /** * 截取擴展名字 * * @param filename * @param defExt * @return */ public static String getExtension(String filename) { if ((filename != null) && (filename.length() > 0)) { int i = filename.lastIndexOf('.'); if ((i > -1) && (i < (filename.length() - 1))) { return filename.substring(i); } } return ""; } }
下載地址 http://pan.baidu.com/s/1pK7qdLp
version 1.2 版本:http://pan.baidu.com/s/1boNnLoV