写文件操作为什么在LINUX和WINDOWS下的结果会不一样?!!?

MAX°孟兆 2005-05-23 07:00:43
我的代码如下:

try
{
FileOutputStream fo = new FileOutputStream("/home/root/1");
fo.write("你好abc".getBytes());
fo.close();
}
catch (IOException e)
{
e.printStackTrace();
}

听说是系统字符集的问题,通过设置JAVA的系统属性可以解决,但我修改代码如下还是没有任何改变:
try
{
System.setProperty("file.encoding", "GBK");
System.setProperty("user.language", "zh");
Properties pro = System.getProperties();
pro.list(System.out); // <--输入的值显示系统属性已被改变

FileOutputStream fo = new FileOutputStream("/home/maxchou/1");
fo.write("你好abc".getBytes());
fo.close();
}
catch (IOException e)
{
e.printStackTrace();
}
...全文
278 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
PoemCode 2005-05-27
  • 打赏
  • 举报
回复
楼上的是正解
li_haizhou 2005-05-27
  • 打赏
  • 举报
回复
fo.write("你好abc".getBytes("UTF-8"));
MAX°孟兆 2005-05-23
  • 打赏
  • 举报
回复
我意思是:
windows列出的System.getProperties()中
file.encoding=GBK
user.language=zh

而linux列出的System.getProperties()中
file.encoding=UTF-8
user.language=en

这种情况下运行程序后生成的文件分别是GBK编码和UTF-8编码,如何在JAVA中统一其输出的编码。
PoemCode 2005-05-23
  • 打赏
  • 举报
回复
在我的系统ubuntu上没有问题。无中文乱码。
home/root/1--->路径确实存在吗?且你有写权限吗?
不知道你有什么问题?
li_haizhou 2005-05-23
  • 打赏
  • 举报
回复
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/home/maxchou/l")));
bw.write("阿土");
bw.close();

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧