本文介绍了FileOutputStream将编码设置为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个想要将文件设置为utf-8
a want to set a file to utf-8
FileOutputStream仅带有两个参数
the FileOutputStream takes just two parameter
我的代码是
PrintWriter kitaba1 = null;
try {
kitaba1 = new PrintWriter(new FileOutputStream(new File(ismmilaf), true ));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
//kitaba1.println(moarif1);
kitaba1.close();
推荐答案
您需要使用OutputStreamWriter
,以便可以指定输出字符集.然后,如果需要打印语义,则可以将其包装在PrintWriter
或BufferedWriter
中:
You need to use OutputStreamWriter
so you can specify an output charset. You can then wrap that in a PrintWriter
or BufferedWriter
if you need printing semantics:
PrintWriter kitaba1 = null;
try {
kitaba1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(ismmilaf), true), StandardCharsets.UTF_8));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
//kitaba1.println(moarif1);
kitaba1.close();
BufferedWriter kitaba1 = null;
try {
kitaba1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(ismmilaf), true), StandardCharsets.UTF_8));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
//kitaba1.write(moarif1);
//kitaba1.newLine()
kitaba1.close();
这篇关于FileOutputStream将编码设置为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!