如何用Java编写UTF-8文件? [英] How to write a UTF-8 file with Java?
问题描述
我有一些当前的代码,问题是它创建一个1252代码页文件,我想迫使它创建一个UTF-8文件
任何人都可以帮助我与此代码,正如我所说的,目前的工作...但我需要强制保存在utf ..我可以传递一个参数或东西??
这是我有什么,真的赞赏任何帮助
var out = new java.io.FileWriter(new java.io.File(path )),
text = new java.lang.String(src ||);
out.write(text,0,text.length());
out.flush();
out.close();
使用 FileWriter
,创建一个 FileOutputStream
。然后你可以把它封装在一个 OutputStreamWriter
中,它允许你在构造函数中传递一个编码。然后你可以把你的数据写到:
pre $ Output $ Output
I have some current code and the problem is its creating a 1252 codepage file, i want to force it to create a UTF-8 file
Can anyone help me with this code, as i say it currently works... but i need to force the save on utf.. can i pass a parameter or something??
this is what i have, any help really appreciated
var out = new java.io.FileWriter( new java.io.File( path )),
text = new java.lang.String( src || "" );
out.write( text, 0, text.length() );
out.flush();
out.close();
Instead of using FileWriter
, create a FileOutputStream
. You can then wrap this in an OutputStreamWriter
, which allows you to pass an encoding in the constructor. Then you can write your data to that:
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8)
这篇关于如何用Java编写UTF-8文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!