使用Java更改现有文件的编码? [英] Change encoding of existing file with Java?
本文介绍了使用Java更改现有文件的编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以编程方式将一组* nix脚本的编码从Java更改为UTF-8。我不会写任何东西给他们,所以我试图找到最简单的|最快的方法来做到这一点。文件不是太多,并不是那么大。我可以:
I need to programatically change the encoding of a set of *nix scripts to UTF-8 from Java. I won't write anything to them, so I'm trying to find what's the easiest|fastest way to do this. The files are not too many and are not that big. I could:
- 使用UTF-8设置为编码的OutputStreamWrite一个空字符串
<因为我已经在使用FileUtils(从Apache Commons),我可以阅读|写这些文件的内容,传递UTF-8作为编码
不是很大的事情,但有人遇到这种情况吗?是否有任何方法的缺点?
Not a big deal, but has anyone run into this case before? Are there any cons on either approach?
推荐答案
根据要求,由于您使用commons io,以下是示例代码错误检查风):
As requested, and since you're using commons io, here is example code (error checking to the wind):
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Main {
public static void main(String[] args) throws IOException {
String filename = args[0];
File file = new File(filename);
String content = FileUtils.readFileToString(file, "ISO8859_1");
FileUtils.write(file, content, "UTF-8");
}
}
这篇关于使用Java更改现有文件的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文