先上代码吧
dd.properties文件()放在d盘house=5套
car=大众
money=500万Proper.java文件import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Proper {
public static void main(String[] args) {
Proper pp=new Proper();
Properties props = new Properties();
try {
InputStream in=new FileInputStream("d://dd.properties");
BufferedReader bf=new BufferedReader(new InputStreamReader(in));
props.load(bf);
String house=props.getProperty("house");house=pp.changeChineseCode(house);
String car=props.getProperty("car");car=pp.changeChineseCode(car);
String money=props.getProperty("money");money=pp.changeChineseCode(money);
System.out.println("house: "+house+" car: "+car+" money: "+money);
String vv="10通天塔";
// String vv2=new String(vv.getBytes("UTF-8"),"GBK");
// String vv2=new String(vv.getBytes("UTF-8"),"GB2312");
// String vv2=new String(vv.getBytes("UTF-8"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("UTF-8"),"Unicode");
// String vv2=new String(vv.getBytes("GBK"),"UTF-8");
// String vv2=new String(vv.getBytes("GBK"),"GB2312");
// String vv2=new String(vv.getBytes("GBK"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("GBK"),"Unicode");
// String vv2=new String(vv.getBytes("GB2312"),"UTF-8");
// String vv2=new String(vv.getBytes("GB2312"),"GBK");
// String vv2=new String(vv.getBytes("GB2312"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("GB2312"),"Unicode");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"UTF-8");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"GBK");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"GB2312");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"Unicode");
// String vv2=new String(vv.getBytes("Unicode"),"UTF-8");
// String vv2=new String(vv.getBytes("Unicode"),"GBK");
// String vv2=new String(vv.getBytes("Unicode"),"ISO-8859-1");
String vv2=new String(vv.getBytes("Unicode"),"GB2312");
props.setProperty("house", vv2);
props.setProperty("car", "6");
props.setProperty("money", "1000000000000000000");
props.store(new FileOutputStream("d://dd.properties"), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
house=5套
car=大众
money=500万
*/
//ISO-8859-1 GB2312 GBK UTF-8 Unicode
public String changeChineseCode(String oldStr){
String value=null;
try {
value =new String(oldStr.getBytes("ISO-8859-1"),"gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}无论是读取还是写文件中文都是乱码,我已经尝试过改变各种编码方式了,注释掉的部分就是我试过的
所以不要跟我讲去改变编码了,网上也有一种使用cmd命令的我也是过了,不管用,哪个方法也不实用,
用户不会去用cmd命令的。希望高手帮我解答,请实验成功后告诉我,我不喜欢别人靠感觉说话,多谢!
dd.properties文件()放在d盘house=5套
car=大众
money=500万Proper.java文件import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Proper {
public static void main(String[] args) {
Proper pp=new Proper();
Properties props = new Properties();
try {
InputStream in=new FileInputStream("d://dd.properties");
BufferedReader bf=new BufferedReader(new InputStreamReader(in));
props.load(bf);
String house=props.getProperty("house");house=pp.changeChineseCode(house);
String car=props.getProperty("car");car=pp.changeChineseCode(car);
String money=props.getProperty("money");money=pp.changeChineseCode(money);
System.out.println("house: "+house+" car: "+car+" money: "+money);
String vv="10通天塔";
// String vv2=new String(vv.getBytes("UTF-8"),"GBK");
// String vv2=new String(vv.getBytes("UTF-8"),"GB2312");
// String vv2=new String(vv.getBytes("UTF-8"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("UTF-8"),"Unicode");
// String vv2=new String(vv.getBytes("GBK"),"UTF-8");
// String vv2=new String(vv.getBytes("GBK"),"GB2312");
// String vv2=new String(vv.getBytes("GBK"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("GBK"),"Unicode");
// String vv2=new String(vv.getBytes("GB2312"),"UTF-8");
// String vv2=new String(vv.getBytes("GB2312"),"GBK");
// String vv2=new String(vv.getBytes("GB2312"),"ISO-8859-1");
// String vv2=new String(vv.getBytes("GB2312"),"Unicode");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"UTF-8");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"GBK");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"GB2312");
// String vv2=new String(vv.getBytes("ISO-8859-1"),"Unicode");
// String vv2=new String(vv.getBytes("Unicode"),"UTF-8");
// String vv2=new String(vv.getBytes("Unicode"),"GBK");
// String vv2=new String(vv.getBytes("Unicode"),"ISO-8859-1");
String vv2=new String(vv.getBytes("Unicode"),"GB2312");
props.setProperty("house", vv2);
props.setProperty("car", "6");
props.setProperty("money", "1000000000000000000");
props.store(new FileOutputStream("d://dd.properties"), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
house=5套
car=大众
money=500万
*/
//ISO-8859-1 GB2312 GBK UTF-8 Unicode
public String changeChineseCode(String oldStr){
String value=null;
try {
value =new String(oldStr.getBytes("ISO-8859-1"),"gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}无论是读取还是写文件中文都是乱码,我已经尝试过改变各种编码方式了,注释掉的部分就是我试过的
所以不要跟我讲去改变编码了,网上也有一种使用cmd命令的我也是过了,不管用,哪个方法也不实用,
用户不会去用cmd命令的。希望高手帮我解答,请实验成功后告诉我,我不喜欢别人靠感觉说话,多谢!
解决方案 »
- 今天(国庆)那几个论坛可以免费注册?
- 注册弹出新窗口
- 复选框打钩删除动态链表问题!
- 菜鸟提问1
- 请教这是什么问题
- jsf可以动态生成组件id吗?
- 哪为朋友知道SessionFactory.java中session = sessionFactory.openSession()是干什么用的?老是这句出错!在线候着
- 关于jboss服务器的问题
- 发布EJB时出现的错误,详见内容?
- weblogic7+sqlserver2000+win2kAS+ejb2.0开发b/s结构程序时,weblogic报错如下:
- 一道关于JSTL的小题,在线等
- spring3mvc + hibernate + freemarker + jquery + json想看就进来吧
2.删除house=pp.changeChineseCode(house);
3.删除String vv2=new String(vv.getBytes("Unicode"),"GB2312");
4.props.setProperty("house", vv2); 第二参数改成 vv
运行后生成的属性文件不乱码
public class Proper { public static void main(String[] args) {
Proper pp = new Proper();
Properties props = new Properties();
try {
InputStream in = new FileInputStream("E:/eclipse/workspace/poi/bin/classes/dd.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
props.load(in); String house = props.getProperty("house");
String car = props.getProperty("car");
String money = props.getProperty("money"); System.out.println("house: " + house + " car: " + car + " money: " + money); String vv = "10通天塔"; props.setProperty("house", vv);
props.setProperty("car", "6");
props.setProperty("money", "1000000000000000000"); props.store(new FileOutputStream("d://dd.properties"), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Proper { public static void main(String[] args) {
Proper pp=new Proper();
Properties props = new Properties();
try {
InputStream in=new FileInputStream("d://dd.properties");
BufferedReader bf=new BufferedReader(new InputStreamReader(in,"gbk"));
props.load(bf); String house=props.getProperty("house");
String car=props.getProperty("car");
String money=props.getProperty("money"); System.out.println("house: "+house+" car: "+car+" money: "+money); String vv="通天塔";// String vv2=new String(vv.getBytes("Unicode"),"GB2312"); props.setProperty("house", vv);
props.setProperty("car", "6");
props.setProperty("money", "1000000000000000000"); props.store(new FileOutputStream("d://dd.properties"), null);
in.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
楼主需要将\u编码放到文件中。
Properties props = new Properties();
try {
InputStream in = new FileInputStream("d:\\dd.properties"); // GBK编码的文本文件。最好不要用.properties文件名后缀,会让别人误解的
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String aLine;
while ((aLine = bf.readLine()) != null)
sb.append(aLine + "\n");
bf.close();
StringBuffer sb2 = new StringBuffer();
for (char c : sb.toString().toCharArray()) {
int value = (int) c;
if (value < 0xFF)
sb2.append(c);
else {
sb2.append("\\u");
sb2.append(Integer.toHexString(value).toUpperCase());
}
}
props.load(new ByteArrayInputStream(sb2.toString().getBytes())); String house = props.getProperty("house");
String car = props.getProperty("car");
String money = props.getProperty("money"); System.out.println("house: " + house + "\n car: " + car + "\n money: "
+ money); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
new InputStreamReader(in,"GB2312"));
或者用工具变成unicode之后在粘贴在资源文件中。
代码是:InputStream in=new FileInputStream("d://dd.properties");
InputStreamReader re=new InputStreamReader(in,"ISO-8859-1");
BufferedReader bf=new BufferedReader(re);
props.load(bf);
String house=props.getProperty("house");house=pp.changeChineseCode(house);
String car=props.getProperty("car");car=pp.changeChineseCode(car);
String money=props.getProperty("money");money=pp.changeChineseCode(money);
System.out.println("house: "+house+" car: "+car+" money: "+money);
但没有说怎么写,写入文件时还是乱码,我想用7楼的方法解决写入中文乱码问题但是不行,因为store(OutputStream out, String comments) 方法传入的是OutputStream对象,不是OutputStreamWriter对象,也就是OutputStreamWriter wr=new OutputStreamWriter(os,"ISO-8859-1")没法用
我已经很明确说了,你是文件编码的问题,你自己不懂别怪别人,你配置文件用的编码格式是什么,你自己知道么?我这里跑GBK编码好好的,你那里不行 你也不说什么配置文件,谁管的了你 ,这点P问题懒得说你
native2ascii -encoding utf-8 messages_zh_CN.txt messages_zh_CN.propertieshttp://blog.csdn.net/dzr1990/article/details/6055964
这篇文章你可以看一下,不知道对你有不有帮助另外我想说一句,我没感觉二楼说的哪里不好,他说的那个方法你知道吗?就算你知道,像我这种菜鸟也是不知道的,他能帮助我们这些菜鸟增长知识没什么不对,你不要只站在你自己的立场想问题。最后,两位哥不要吵了,大家淡定……
②正规的.properties文件中是否能放GBK,14楼火龙果也有支持的,这是常识,这一点不想争论。也许你的意思是你放的不是GBK,那顶楼的帖子中明摆着文件中有中文,中文在正规.properties文件中就应该是\uXXXX编码的。
③如果说7楼的代码可以,你应该是修改了默认字符集。楼主可以在原始中文环境cmd窗口下运行这些个程序试试看。技术问题,心平气和来解决,我是帮忙的,不是为难挑刺的。你的回复如若再有火气,我就不好意思再帮你了。