我想我需要使用
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"));
但是当我将代码更改为 BufferedWriter 时,该行出现了问题out.write(e);
。
我的代码:
public void writeToFile(String fileName, List<ThongtinKhachThue> resultttkt){
System.out.println("Begin....");
try {
PrintWriter out = new PrintWriter(new File(fileName));
out.println("//thue.txt");
for(ThongtinKhachThue e : resultttkt){
out.println(e);
}
out.println("Tổng khách thuê phòng: "+TongKhachThue());
out.println("Tổng tiền thu: "+TongTienThuDuoc()+"$");
out.flush();
out.close();
System.out.println("End...");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
它在文件中显示的内容:
//thue.txt
K1, A1002, 47.05$
K2, A0003, 67.0$
K3, A1001, 31.0$
K4, null, 0.0$
T?ng khách thuê ph?ng: 3
T?ng ti?n thu: 145.05$
我希望它显示的是:
//thue.txt
K1, A1002, 47.05$
K2, A0003, 67.0$
K3, A1001, 31.0$
K4, Không thuê
Tổng khách thuê: 03
Tổng tiền thu: 145.05$
如何将 utf-8 写入文件并将“3”更改为“03”?