wustguangh |分类标签:Java, 文件操作 2016-01-10
Java如何以指定的字符编码保存文件?
如果需要指定字符编码,如UTF-8或者GBK,使用Java进行文件保存时如何实现呢?
Ctrl+Enter直接提交
回答(1)
wustguangh 19级 2016-01-10 08:39:17
private String file = "D:/test.html"; private void saveAsFileWriter(String content) { OutputStreamWriter fwriter = null; try { fwriter = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); fwriter.write(content); } catch (IOException ex) { ex.printStackTrace(); } finally { try { fwriter.flush(); fwriter.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Java文件操作详细介绍,建议参考:Java文件操作方法总结