我有一个text文件,格式如下:
000001 中文名1
000002 中文名2
... ...利用Properties类,load之后读取,结果中文都是乱码或者问号,请问这个该如何解决?
text文件另存为UTF-8或者其他格式都无效。读出的字符串也试过了各种编码和解码都不能解决。
000001 中文名1
000002 中文名2
... ...利用Properties类,load之后读取,结果中文都是乱码或者问号,请问这个该如何解决?
text文件另存为UTF-8或者其他格式都无效。读出的字符串也试过了各种编码和解码都不能解决。
老文件需要保留着,便于以后更改,更改之后重新生成一下就可以了,建议
可以做个 bat 文件。native2ascii <源文件> <新文件>
/**
* 将ISO-8859-1转化为GBK
*
* @param para
* @return
*/
public static String isoToGbk(String para) {
try {
return new String(para.getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
return "";
} catch (Exception e) {
return "";
}
}
3楼的方法,我这个环境不支持GBK编码,我试过gb2312,也是乱码。现在情况是:
txt文件是存为UTF-8
java.util.Properties.load(stream);
String oriName = properties.getProperty(id);
String name = new String(oriName.getBytes("ISO8859-1"),"gb2312"); 上面的参数中,我尝试过多种charset,文件也用NATIVE2ASCII转换过,并且文本文件
也尝试另存为其他格式比如Unicode,统统不行,痛苦。
要是txt文件不如直接读呢那样就没问题了
text文件格式就是我前面说的,上千行:00001 中文名1
00002 中文名2
....如果直接读,请问怎么效率高一些
这样试试,自己用输入输出流控制
Properties p = new Properties();
InputStreamReader isr = new InputStreamReader(new FileInputStream("your_property_file"), "UTF-8"); //用utf-8流读入文件
int len = 1048;
char[] buf = new char[len];
PipedOutputStream pos = new PipedOutputStream();//输出管道
PrintStream ps = new PrintStream(pos, false, "UTF-8");//输出流PipedInputStream pis = new PipedInputStream(pos); //输入管道
while (irs.read(buf, 0, len) != -1) {
ps.print(buf); //把读入文件输出到管道,从而得到新的管道输入流
}
ps.flash();
p.load(pis); //用新的管道输入流载入property具体行不行不知道,试试看吧
[code=BatchFile]
set path=%path%;C:\jdk1.5\bin
native2ascii -encoding gb2312 ApplicationResources_temp.properties ApplicationResources_zh_CN.propertiesnative2ascii -encoding gb2312 displaytag_temp.properties displaytag_zh_CN.propertiesnative2ascii -encoding gb2312 eduadminResources_temp.properties eduadminResources_zh_CN.propertiesnative2ascii -encoding gb2312 studentResources_temp.properties studentResources_zh_CN.properties
[/code]
public synchronized void load(InputStream inStream) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "GBK"));
text文件格式就是我前面说的,上千行: 00001 中文名1
00002 中文名2
.... 如果直接读,请问怎么效率高一些
_____________________不同问题,不同分析,你要把这些读到什么地方,用这些做什么?
native2ascii.exe -encoding utf-8 .\src\Movision\Resource.properties .\classes\Movision\Resource.properties
(注意:properties文本用GB2312编码)
第二步:
public static void init() {
if (resources == null) {
resources = ResourceBundle.getBundle(sResourcesName);
}
} /**
* @param sKeyName 被读取的Key=Value对
* @return 返回sKeyName对应的值
*/
public static String getString(String sKeyName) {
String sResult = null;
try {
init();
sResult = resources.getString(sKeyName);
}
catch (Exception e) {
e.printStackTrace();
sResult = null;
}
return sResult;
}以上代码通过实践检验可行。
先把存进去的格式变换为GB2312或者GBK
再取出来的时候就不会是乱码了
fso.CreateTextFile(Server.MapPath(File1.Value), true, true);
string str = TextBox1.Text.Trim();
StreamWriter sw = new StreamWriter(File1.Value, false, Encoding.UTF8);
sw.Write(str);
sw.Dispose();