加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Multiple strings.xml files cause order and incomplete proble

发布时间:2020-12-16 05:52:39 所属栏目:百科 来源:网络整理
导读: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 sol

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:workspaceTimeSheetres
	 * @param defaultFilePath  = E:workspaceTimeSheetresvaluesstrings.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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读