Android @1x,@2x,@3x 资源文件自动分包工具

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

猜你喜欢

转载自www.cnblogs.com/zhujiabin/p/9390519.html
今日推荐