there is a boring task,boss let me check multiple strings.xml files to findthe differences compare to res/values/strings.xml. Jesus christ! It's 33 kinds of languages in android. I do not want work like a idiot. so,I wrote a program to solve that stupid task.
here is my code.
import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 思路: * 给定一个res路径,遍历读取以values-开头的目录存到集合中 * 读取目录下的strings.xml文件,将数据转为键值对HashMap<String,String> source---("XXX","<string name="XXX">ZZZZ</string>"); * 遍历values/strings.xml默认文件,逐行读取String readLine,同时声明一个List<String> tem; * 如果该行为<string name="XXX">ZZZZ</string>则提取键XXX,如果匹配到source则tem.add(source.get(key)); * 如果该行没有key(注释)或者XML文件头,或者没有匹配的则直接tem.add(readLine); * * 注意事项: * 不能在安卓项目中使用! 需要新建一个Java project运行,导入的包全为java.nio.* 下的子类 * 如果报错则添加JRE Library * @author jack * last-modify : 2016-4-19 12:06:35 */ public class StringsFileUtil { /** * @param resFileDir = E:\\workspace\\TimeSheet\\res * @param defaultFilePath = E:\\workspace\\TimeSheet\\res\\values\\strings.xml * @throws IOException */ public void tidyFiles(String resFileDir,String defaultFilePath) throws IOException{ final Path path = Paths.get(resFileDir); final Path dataPath = Paths.get(defaultFilePath); final List<String> lines = Files.readAllLines(dataPath,Charset.forName("utf-8")); Path absPath = path.toAbsolutePath(); Files.walkFileTree(absPath,new FileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir,IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) throws IOException { if (dir.getName(dir.getNameCount()-1).toString().startsWith("values-")) { // System.out.println(dir); return FileVisitResult.CONTINUE; }else if(dir.equals(path)){ return FileVisitResult.CONTINUE; } return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith("strings.xml")) { // System.out.println(file); new TransferThread(lines,file).start(); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file,IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } } class TransferThread extends Thread{ private List<String> data; private Path path; public TransferThread(List<String> data,Path path) { this.data = data; this.path = path; } @Override public void run() { Map<String,String> source; try { source = getFileKeyValues(path); List<String> tem = new ArrayList<>(); for (String readLine : data) { String[] data = readLine.split("\""); if (data.length == 3) { String key = data[1]; if (source.containsKey(key)) { tem.add(source.get(key)); tem.remove(key); continue; } } tem.add(readLine); } Files.write(path,tem,Charset.forName("utf-8")); System.out.println("------finish "+path); } catch (IOException e) { e.printStackTrace(); } } public static Map<String,String> getFileKeyValues(Path path) throws IOException { Map<String,String> source = new HashMap<String,String>(); List<String> lines = Files.readAllLines(path,Charset.forName("utf-8")); for (String it : lines) { String[] data = it.split("\""); if (data.length == 3) { String key = data[1]; data = it.split(">"); if (data.length > 1) { source.put(key,it); // System.out.println("key=" + key + ",value=" + it); } } } return source; } }
I'm fish,I'm on.
原文链接:https://www.f2er.com/xml/295329.html