Properties乱码问题 不算乱码吧,只是把中文转为了 unicode 进行存储而已 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你说的乱码是这个吗?Frequency=\u54c8\u54c8这是改不了的,除非你不用Properties类,因为在Properties类里已经写死了: private void dumpString(StringBuilder buffer, String string, boolean key) { int i = 0; if (!key && i < string.length() && string.charAt(i) == ' ') { buffer.append("\\ "); i++; } for (; i < string.length(); i++) { char ch = string.charAt(i); switch (ch) { case '\t': buffer.append("\\t"); break; case '\n': buffer.append("\\n"); break; case '\f': buffer.append("\\f"); break; case '\r': buffer.append("\\r"); break; default: if ("\\#!=:".indexOf(ch) >= 0 || (key && ch == ' ')) { buffer.append('\\'); } if (ch >= ' ' && ch <= '~') { buffer.append(ch); } else { String hex = Integer.toHexString(ch); buffer.append("\\u"); for (int j = 0; j < 4 - hex.length(); j++) { buffer.append("0"); } buffer.append(hex); } } } } 这应该对你没有影响吧,因为Properties的加载和保存都自动处理编码了。你是想自己解析Properties生成的文件吗?那倒不如用json作为文件格式了。 如果一定要中文的话,你就自己手动输出文件不就好了public void saveConfig(String file, Properties properties) { try { FileOutputStream s = new FileOutputStream(file, false); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s, "utf-8")); //在这里手动遍历属性和值进行输出 s.close(); } catch (Exception e){ e.printStackTrace(); } }但是如果这样输出的话,你再使用Properties进行读取加载,可能不能读到你想要的东西 因为这个东西要拿出来给别人用的。所以要显示中文的。那用json格式很方便啊。 因为这个东西要拿出来给别人用的。所以要显示中文的。那用json格式很方便啊。json写到文件里面不是a=b这种格式 因为这个东西要拿出来给别人用的。所以要显示中文的。那用json格式很方便啊。json写到文件里面不是a=b这种格式为什么一定要a=b的格式,直接用json就可以,现在没有那种主流语言不支持json吧。如果你一定要用properties的格式,那就只能自己转一下了。直接用android类库是做不了了,因为properties把代码写死了。 所谓的数据储存,读取,不过是双方相互之间规定某种特定格式的文本进行传输。不必太在意格式问题,什么gson,json,xml,都可以。只要你知道怎么解析就好了,如果你希望方便使用,不希望自己创建类进行解析生成 什么的,json,xml这种很多语言支持的自然是首选,如果你向自己使用解析,你就可以随便写了,只要自己知道怎么解析,是什么含义就好了 同样的代码,在java上,不在android上运行,拿出文档来看就不是unicode编码的。public class test123 { public static void main(String[] args) { Properties prop = new Properties(); int issueType = 1; int frequency = -1; String description = "123131234124"; String contactInfo = "1234567"; prop.setProperty("IssueType", "呵呵"); prop.setProperty("Frequency", "12345"); prop.setProperty("Description", description); prop.setProperty("ContactInfo", contactInfo); saveConfig("c:/1.txt", prop); } static public void saveConfig(String file, Properties properties) { try { FileOutputStream s = new FileOutputStream(file, false); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s, "utf-8")); properties.store(bw, ""); s.close(); } catch (Exception e){ e.printStackTrace(); } }} android和java还是不一样的,android为了很多效率以及别的因素对java本身的库函数做了很多修正的,所以使用android的java类库的时候,并不是完全和oracle的类库一样 麻烦大牛说说android怎么进一部提升 如果读取格式化数据从文件中? Android初学者求指教 大家帮我看看这是怎么回事啊?找了好久了 取得EDTEXT的值。 windows上eclipse+CDT开发C++项目没有自动提示功能 只需要借你十秒钟,来审阅一下我的代码哪里有问题 android DBhelper 简单问题 listview中好看的分割线的样式有哪些? 新手今天的心得分享(书籍《第一行代码 Android》) make all Cannot run program "make": Launching failed 新手求助adt更新问题
int i = 0;
if (!key && i < string.length() && string.charAt(i) == ' ') {
buffer.append("\\ ");
i++;
} for (; i < string.length(); i++) {
char ch = string.charAt(i);
switch (ch) {
case '\t':
buffer.append("\\t");
break;
case '\n':
buffer.append("\\n");
break;
case '\f':
buffer.append("\\f");
break;
case '\r':
buffer.append("\\r");
break;
default:
if ("\\#!=:".indexOf(ch) >= 0 || (key && ch == ' ')) {
buffer.append('\\');
}
if (ch >= ' ' && ch <= '~') {
buffer.append(ch);
} else {
String hex = Integer.toHexString(ch);
buffer.append("\\u");
for (int j = 0; j < 4 - hex.length(); j++) {
buffer.append("0");
}
buffer.append(hex);
}
}
}
}
你是想自己解析Properties生成的文件吗?那倒不如用json作为文件格式了。
try {
FileOutputStream s = new FileOutputStream(file, false);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s, "utf-8"));
//在这里手动遍历属性和值进行输出
s.close();
} catch (Exception e){
e.printStackTrace();
}
}但是如果这样输出的话,你再使用Properties进行读取加载,可能不能读到你想要的东西
因为这个东西要拿出来给别人用的。所以要显示中文的。
那用json格式很方便啊。
因为这个东西要拿出来给别人用的。所以要显示中文的。
那用json格式很方便啊。
json写到文件里面不是a=b这种格式
因为这个东西要拿出来给别人用的。所以要显示中文的。
那用json格式很方便啊。
json写到文件里面不是a=b这种格式
为什么一定要a=b的格式,直接用json就可以,现在没有那种主流语言不支持json吧。
如果你一定要用properties的格式,那就只能自己转一下了。直接用android类库是做不了了,因为properties把代码写死了。
同样的代码,在java上,不在android上运行,拿出文档来看就不是unicode编码的。public class test123 {
public static void main(String[] args) {
Properties prop = new Properties();
int issueType = 1;
int frequency = -1;
String description = "123131234124";
String contactInfo = "1234567";
prop.setProperty("IssueType", "呵呵");
prop.setProperty("Frequency", "12345");
prop.setProperty("Description", description);
prop.setProperty("ContactInfo", contactInfo);
saveConfig("c:/1.txt", prop); } static public void saveConfig(String file, Properties properties) {
try {
FileOutputStream s = new FileOutputStream(file, false);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s, "utf-8"));
properties.store(bw, "");
s.close();
} catch (Exception e){
e.printStackTrace();
}
}
}