一、创建置换工具
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URISyntaxException; /** * 字符转换工具 */ public class Native2Ascii { private static String fromFile = null; private static String toFile = null; private static String toSrcFile = null; /** * 将字符串str中的非Ascii字符转换为\uFFFF格式 * * @param str * 需要转换的字符 * @return 转换结果 */ public static String toAscii(String str) { if (str.trim().startsWith("#")) return str; StringBuffer re = new StringBuffer(); char[] chs = str.toCharArray(); for (char c : chs) { if (c > 255) re.append("\\u" + Integer.toHexString(c)); else re.append(c); } return re.toString(); } /** * Native2Ascii 原文件 新文件路径 * * @throws URISyntaxException */ public static void main(String args[]) throws IOException, URISyntaxException { if (args == null || args.length == 0) { File file = new File(Native2Ascii.class.getResource("/N2A.ini") .toURI()); if (!file.exists()) { file.createNewFile(); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write("#从资源文件"); writer.newLine(); writer.write("From File = "); writer.newLine(); writer.write("#到资源文件"); writer.newLine(); writer.write("To File = "); writer.newLine(); writer.write("#到本地源文件"); writer.newLine(); writer.write("Src File = "); writer.newLine(); writer.close(); System.out.println("配置文件不存在,以默认方式创建 \"N2A.ini\" 文件"); return; } else { InputStream in = Native2Ascii.class .getResourceAsStream("/N2A.ini"); if (!readConfig(in)) return; } } else if (args == null || args.length != 3) { System.out.println("Native2Ascii 原文件 新文件路径"); return; } else { fromFile = args[0]; toFile = args[1]; toSrcFile = args[2]; } BufferedReader reader = new BufferedReader(new InputStreamReader(Native2Ascii.class.getResourceAsStream(fromFile))); BufferedWriter writer = new BufferedWriter(new FileWriter(new File(Native2Ascii.class.getResource(toFile).toURI()))); BufferedWriter srcWriter = new BufferedWriter(new FileWriter(toSrcFile)); String temp; while ((temp = reader.readLine()) != null) { temp=toAscii(temp); writer.write(temp); writer.newLine(); srcWriter.write(temp); srcWriter.newLine(); } reader.close(); writer.close(); srcWriter.close(); } private static boolean readConfig(InputStream in) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(in)); String temp,name,key; int index,line=0; while((temp=reader.readLine())!=null){ line++; if(temp.trim().length()==0)continue; if(temp.trim().startsWith("#"))continue; if(temp.trim().startsWith("!"))continue; index=temp.indexOf("="); if(index==-1){ System.out.println("Error: Line "+line+" is not format \"name=value\""); continue; } name=temp.substring(0, index).trim(); key=temp.substring(index+1).trim(); if(name.equalsIgnoreCase("From File")){ fromFile=key; }else if(name.equalsIgnoreCase("To File")){ toFile=key; }else if(name.equalsIgnoreCase("Src File")){ toSrcFile=key; } } boolean ok=true; if(fromFile==null||fromFile.length()==0){ ok=false; System.out.println("Error: \"From File\" value is NULL"); } if(toFile==null||toFile.length()==0){ ok=false; System.out.println("Error: \"To File\" value is NULL"); } if(toSrcFile==null||toSrcFile.length()==0){ ok=false; System.out.println("Error: \"Src File\" value is NULL"); } return ok; } } |
二、注意此工具的配置文件N2A.ini
#资源文件 的源文件 From File = /MyResources.txt #Struts配置的资源文件 To File = /MessageResources.properties #Struts配置的资源文件的源文件 Src File = src/MessageResources.properties |
三、效果
/MyResources.txt 文件内容如下:
#全局信息 system.name = 影城管理系统 system.version = Vesion 1.0 system.copy = 2008-2020 page.title = 欢迎进入影城管理系统 type.null = 请求类型不能为空 |
运行置换工具得到文件:/MessageResources.properties src/MessageResources.properties 文件内容如下:
#全局信息 system.name = \u5f71\u57ce\u7ba1\u7406\u7cfb\u7edf system.version = Vesion 1.0 system.copy = 2008-2020 page.title = \u6b22\u8fce\u8fdb\u5165\u5f71\u57ce\u7ba1\u7406\u7cfb\u7edf type.null = \u8bf7\u6c42\u7c7b\u578b\u4e0d\u80fd\u4e3a\u7a7a |