如何用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编码,怎么办??
补充:这里写入的字符包括中文的
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编码,怎么办??
补充:这里写入的字符包括中文的
FileOutputStream fos = new FileOutputStream(path);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(content);
out.close();
fos.close();