我有一些当前代码,问题是它创建了一个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,以允许你在构造函数中传递编码。然后,你可以将数据写入try-with-resources语句中:
FileWriter
FileOutputStream
OutputStreamWriter
try-with-resources
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8)) // do stuff }
尝试这个
Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("outfilename"), "UTF-8")); try { out.write(aString); } finally { out.close(); }