java:如何将文件转换为utf8 [英] java: how to convert a file to utf8

查看:477
本文介绍了java:如何将文件转换为utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件有一些非utf8字符(如ISO-8859-1),所以我想将该文件(或读取)转换为UTF8编码,我该怎么做?

i have a file that have some non-utf8 caracters (like "ISO-8859-1"), and so i want to convert that file (or read) to UTF8 encoding, how i can do it?

代码如下:

File file = new File("some_file_with_non_utf8_characters.txt");

/* some code to convert the file to an utf8 file */

...

编辑:放置编码示例

推荐答案

  String charset = "ISO-8859-1"; // or what corresponds
  BufferedReader in = new BufferedReader( 
      new InputStreamReader (new FileInputStream(file), charset));
  String line;
  while( (line = in.readLine()) != null) { 
    ....
  }

你有解码的文字。您可以使用simmetric Writer / OutputStream方法,使用您喜欢的编码(例如UTF-8)来编写它。

There you have the text decoded. You can write it, by the simmetric Writer/OutputStream methods, with the encoding you prefer (eg UTF-8).

这篇关于java:如何将文件转换为utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆