自动生成dimens文件

来源:互联网 发布:windows pyqt5 安装 编辑:程序博客网 时间:2024/05/17 06:36

改变newpath 与 i可以自定义生成各个分辨率的demins文件 (需要有一个原文件)

(需要先创建文件夹)运行java文件即可import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class Test {static String newPath ="C:/Users/你的电脑名/Desktop/values-ldpi/dimens.xml";//新文件名static double i=0.75;//倍率      public static void gen() {          //以此文件夹下的dimens.xml文件内容为初始值参照          File file = new File("C:/Users/liulingzhen/Desktop/values/dimens.xml");            BufferedReader reader = null;          StringBuilder newString = new StringBuilder();            try {                System.out.println("生成不同分辨率:");                reader = new BufferedReader(new FileReader(file));                String tempString;                int line = 1;                // 一次读入一行,直到读入null为文件结束                while ((tempString = reader.readLine()) != null) {                      if (tempString.contains("</dimen>")) {                        //tempString = tempString.replaceAll(" ", "");                        String start = tempString.substring(0, tempString.indexOf(">") + 1);                        String end = tempString.substring(tempString.lastIndexOf("<") - 2);                      //截取<dimen></dimen>标签内的内容,从>右括号开始,到左括号减2,取得配置的数字                      Double num = Double.parseDouble                              (tempString.substring(tempString.indexOf(">") + 1,                                       tempString.indexOf("</dimen>") - 2));                        //根据不同的尺寸,计算新的值,拼接新的字符串,并且结尾处换行。                      newString.append(start).append( num * i).append(end).append("\r\n");                     } else {                  newString.append(tempString).append("");                  }                    line++;                }                reader.close();              System.out.println("<!--  new -->");                System.out.println(newString);                String newfile = newPath;               //将新的内容,写入到指定的文件中去              writeFile(newfile, newString.toString());            } catch (IOException e) {                e.printStackTrace();            } finally {                if (reader != null) {                    try {                        reader.close();                    } catch (IOException e1) {                        e1.printStackTrace();                    }                }            }        }          /**      * 写入方法      *      */        public static void writeFile(String file, String text) {            PrintWriter out = null;            try {                out = new PrintWriter(new BufferedWriter(new FileWriter(file)));                out.println(text);            } catch (IOException e) {                e.printStackTrace();            }                out.close();        }      public static void main(String[] args) {            gen();        }    }  


0 0
原创粉丝点击