国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Properties類不直接支持多語言屬性文件的解決方法

2019-11-18 14:51:32
字體:
供稿:網(wǎng)友

  PRoperties類不直接支持多語言屬性文件的解決方法

我們在開發(fā)的時(shí)候經(jīng)常需要使用Properties存儲(chǔ)需要的一些屬性,我們往往將需要的屬性存儲(chǔ)在一個(gè)文件里面,然后使用Properties.load()方法加載那些屬性,對于中國的用戶,這些文件一般都是需要包含中文的,但是Properties類的load方法不支持中文或者其他的雙字節(jié)的編碼,我們往往需要使用java的另外一個(gè)工具native2ascii將那個(gè)文件轉(zhuǎn)換為unicode的轉(zhuǎn)義序列的形式,但是這樣一來文件的內(nèi)容又不可讀了,需要我們重復(fù)進(jìn)行轉(zhuǎn)換的工作,有時(shí)候很麻煩,筆者也經(jīng)常碰到這個(gè)問題,后來查看了一下Properties的源代碼,發(fā)現(xiàn)其實(shí)這個(gè)問題很好修正,因?yàn)镻roperties的load方法的第一行代碼規(guī)定了文件的編碼,我們簡單的去掉那個(gè)編碼即可。
JDK的源代碼的第一行語句為:

BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "8859_1"));
我們只去掉后面規(guī)定的那個(gè)編碼就可以實(shí)現(xiàn)平臺(tái)相關(guān)的編碼方式,為了避免直接修改源代碼,我們重新定義一個(gè)類,例如:

package org.jr.util;import java.util.*;import java.io.*;public class Properties extends java.util.Properties { private static final String keyValueSeparators = "=: f"; private static final String strictKeyValueSeparators = "=:"; private static final String specialSaveChars = "=: f#!"; private static final String whiteSpaceChars = " f"; public synchronized void load(InputStream inStream) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); while (true) { // Get next line String line = in.readLine(); if (line == null) { return; } if (line.length() > 0) { // Continue lines that end in slashes if they are not comments char firstChar = line.charAt(0); if ( (firstChar != ´#´) && (firstChar != ´!´)) { while (continueLine(line)) { String nextLine = in.readLine(); if (nextLine == null) { nextLine = ""; } String loppedLine = line.substring(0, line.length() - 1); // Advance beyond whitespace on new line int startIndex = 0; for (startIndex = 0; startIndex < nextLine.length(); startIndex++) { if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1) { break; } } nextLine = nextLine.substring(startIndex, nextLine.length()); line = new String(loppedLine + nextLine); } // Find start of key int len = line.length(); int keyStart; for (keyStart = 0; keyStart < len; keyStart++) { if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1) { break; } } // Blank lines are ignored if (keyStart == len) { continue; } // Find separation between key and value int separatorIndex; for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++) { char currentChar = line.charAt(separatorIndex); if (currentChar == ´/´) { separatorIndex++; } else if (keyValueSeparators.indexOf(currentChar) != -1) { break; } } // Skip over whitespace after key if any int valueIndex; for (valueIndex = separatorIndex; valueIndex < len; valueIndex++) { if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) { break; } } // Skip over one non whitespace key value separators if any if (valueIndex < len) { if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1) { valueIndex++; // Skip over white space after other separators if any } } while (valueIndex < len) { if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) { break; } valueIndex++; } String key = line.substring(keyStart, separatorIndex); String value = (separatorIndex < len) ? line.substring(valueIndex, len) : ""; // Convert then store key and value key = loadConvert(key); value = loadConvert(value); put(key, value); } } } } /* * Returns true if the given line is a line that must * be appended to the next line */ private boolean continueLine (String line) { int slashCount = 0; int index = line.length() - 1; while((index >= 0) && (line.charAt(index--) == ´/´)) slashCount++; return (slashCount % 2 == 1); } /* * Converts encoded /uxxxx to unicode chars * and changes special saved chars to their original forms */ private String loadConvert (String theString) { char aChar; int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len); for(int x=0; x<len; ) { aChar = theString.charAt(x++); if (aChar == ´/´) { aChar = theString.charAt(x++); if(aChar == ´u´) { // Read the xxxx int value=0; for (int i=0; i<4; i++) { aChar = theString.charAt(x++); switch (aChar) { case ´0´: case ´1´: case ´2´: case ´3´: case ´4´: case ´5´: case ´6´: case ´7´: case ´8´: case ´9´: value = (value << 4) + aChar - ´0´; break; case ´a´: case ´b´: case ´c´: case ´d´: case ´e´: case ´f´: value = (value << 4) + 10 + aChar - ´a´; break; case ´A´: case ´B´: case ´C´: case ´D´: case ´E´: case ´F´: value = (value << 4) + 10 + aChar - ´A´; break; default: throw new IllegalArgumentException( "Malformed /uxxxx encoding."); } } outBuffer.append((char)value); } else { if (aChar == ´t´) aChar = ´ ´; else if (aChar == ´r´) aChar = ´

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 衡阳县| 旬邑县| 本溪| 湖口县| 柘城县| 河间市| 浏阳市| 兴国县| 宜良县| 乐山市| 长宁县| 新田县| 金溪县| 商都县| 外汇| 同江市| 六盘水市| 广东省| 漳平市| 贡嘎县| 承德县| 永城市| 扶绥县| 仲巴县| 井研县| 彭山县| 抚州市| 乡宁县| 将乐县| 库伦旗| 壤塘县| 卢湾区| SHOW| 黄龙县| 高唐县| 天镇县| 安吉县| 上高县| 通化县| 馆陶县| 咸阳市|