Java使用utf8格式保存文本文件的方法

时间:2021-10-31 03:43:31

本文实例讲述了Java使用utf8格式保存文本文件的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FileOutputStream fos = null;
OutputStreamWriter writer = null;
try {
  File file = new File(filepath);
  fos = new FileOutputStream(file);
  writer = new OutputStreamWriter(fos,"utf-8");
  writer.write(makeLanguageRes(resource));
} catch (Exception e) {
  CheckXmlUtil.addErrorMsg(CheckXmlUtil.TEXT_FOMART_ERROR,
   CheckXmlUtil.SAVE_XML_FIALL, e);
  return null;
} finally {
  try {
  if(fos != null){
   fos.close();
  }
} catch (IOException e1) {
  CheckXmlUtil.addErrorMsg(CheckXmlUtil.TEXT_FOMART_ERROR,
   CheckXmlUtil.SAVE_XML_FIALL, e1);
}
if (writer != null) {
  try {
   writer.close();
  } catch (IOException e) {
   CheckXmlUtil.addErrorMsg(CheckXmlUtil.TEXT_FOMART_ERROR,
    CheckXmlUtil.SAVE_XML_FIALL, e);
  }
  }
}

希望本文所述对大家java程序设计有所帮助。