View Javadoc
1   package edu.jiangxin.apktoolbox.file.zhconvert;
2   
3   import org.apache.logging.log4j.LogManager;
4   import org.apache.logging.log4j.Logger;
5   
6   import java.io.*;
7   import java.net.URISyntaxException;
8   import java.util.Iterator;
9   import java.util.Properties;
10  
11  public class ZHConverterUtils {
12      private static final Logger logger = LogManager.getLogger(ZHConverterUtils.class.getSimpleName());
13  
14      private Properties charMap = new Properties();
15  
16      private Properties charMap2 = new Properties();
17  
18      public ZHConverterUtils() {
19          String dirpath = "zhSimple2zhTw.properties";
20          String dirpath2 = "zhTw2zhSimple.properties";
21          initProperties(dirpath,charMap);
22          initProperties(dirpath2,charMap2);
23      }
24  
25      private void initProperties(String rPath, Properties properties) {
26          File file = new File(rPath);
27          if (!file.exists()) {
28              try {
29                  file.createNewFile();
30              } catch (IOException e) {
31                  logger.error("createNewFile failed: " + e.getMessage());
32                  return;
33              }
34          }
35  
36          try (InputStream is = new FileInputStream(file);
37               BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
38              properties.load(reader);
39          } catch (IOException e) {
40              logger.error("load failed: " + e.getMessage());
41          }
42      }
43  
44  
45      /**
46       *
47       * @param str
48       * @return
49       */
50      public String myConvertToTW(String str){
51          Iterator iterator = charMap.keySet().iterator();
52          while (iterator.hasNext()){
53              String key = (String) iterator.next();
54              if (key.length() >= 1) {
55                  if (str.contains(key)){
56                      str = str.replaceAll(key,charMap.getProperty(key));
57                  }
58              }
59          }
60          return str;
61      }
62  
63      /**
64       *
65       * @param str
66       * @return
67       */
68      public String myConvertToSimple(String str){
69          Iterator iterator = charMap2.keySet().iterator();
70          while (iterator.hasNext()){
71              String key = (String) iterator.next();
72              if (key.length() >= 1) {
73                  if (str.contains(key)){
74                      str = str.replaceAll(key,charMap2.getProperty(key));
75                  }
76              }
77          }
78          return str;
79      }
80  
81  
82      /**
83       * 加入新词组对应
84       * @param key
85       * @param value
86       */
87      public void storeDataToProperties(String key,String value) {
88          charMap.setProperty(key,value);
89          String filePath = "zhSimple2zhTw.properties";
90          try (OutputStream out = new FileOutputStream(filePath)) {
91              charMap.store(out, "加入新元素");
92          } catch (IOException e) {
93              logger.error("storeDataToProperties failed: IOException");
94          }
95          charMap2.setProperty(value,key);
96          String filePath2 = "zhTw2zhSimple.properties";
97          try (OutputStream out2 = new FileOutputStream(filePath2)) {
98              charMap2.store(out2, "加入新元素");
99          } catch (IOException e) {
100             logger.error("storeDataToProperties failed: IOException");
101         }
102     }
103 
104     public Properties getCharMap() {
105         return charMap;
106     }
107 
108 
109 
110 }