如何用java写文本文件,如下代码:
byte[] b = {';'};
OutputStreamWriter opsw = null;
opsw = new OutputStreamWriter(new FileOutputStream(new File("D:\\My Documents\\Mini2440 My  Documents\\food.txt")));
for(int i=0;i<list.size();i++){
String foodname = list.get(i).getFoodName();
float foodprice = list.get(i).getFoodPrice();
opsw.write(foodname);
opsw.write(";");
opsw.write(String.valueOf(foodprice));
opsw.write(";");
}
opsw.write('\0');
opsw.close();
这里生成的food.txt文件时ANSI编码的,我想要的是Unicode编码,怎么办??
补充:这里写入的字符包括中文的

解决方案 »

  1.   

    给个例子
    FileOutputStream fos = new FileOutputStream(path);
    Writer out = new OutputStreamWriter(fos, "UTF-8");
    out.write(content);
    out.close();
    fos.close();
      

  2.   

    你用的是字节流,要用字符流,你可以去java的API文档去查具体用法
      

  3.   

    1楼正解,OutputStreamWriter的构造方法里面可以选择编码3楼歪解,OutputStreamWriter就是字符流 他是字符流与字节流之间的桥梁 能把字节流转化为字符流 也正因为此 才能选择转化为字符流以后的编码格式