[英]Change encoding of existing file with Java?
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.我需要以编程方式将一组 *nix 脚本的编码从 Java 更改为 UTF-8。我不会向它们写入任何内容,因此我试图找到最简单|最快的方法来执行此操作。 The files are not too many and are not that big.
文件不多也不大。 I could:
我可以:
Not a big deal, but has anyone run into this case before?没什么大不了的,但是以前有人遇到过这种情况吗? Are there any cons on either approach?
这两种方法都有缺点吗?
As requested, and since you're using commons io, here is example code (error checking to the wind):根据要求,并且由于您使用的是公共资源 io,这里是示例代码(错误检查随风):
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.