我有一个文本文件,它是ANSI编码,我必须将其转换为UTF8编码。
我的文字文件是这样的
Stochastic programming is an area of mathematical programming that studies
how to model decision problems under uncertainty. For example, although a
decision might be necessary at a given point in time, essential information
might not be available until a later time.
答案 0 :(得分:7)
您可以使用java.nio.charset.Charset类显式(windows-1252是ANSI的正确名称):
public static void main(String[] args) throws IOException {
Path p = Paths.get("file.txt");
ByteBuffer bb = ByteBuffer.wrap(Files.readAllBytes(p));
CharBuffer cb = Charset.forName("windows-1252").decode(bb);
bb = Charset.forName("UTF-8").encode(cb);
Files.write(p, bb.array());
}
如果您愿意,可以在一行中使用=)
Files.write(Paths.get("file.txt"), Charset.forName("UTF-8").encode(Charset.forName("windows-1252").decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("file.txt"))))).array());
答案 1 :(得分:0)
ASCII字符子集映射到UTF8中的相同字符编码,因此该文件实际上不需要任何转换。
要以UTF-8输出文件,您可以使用:
PrintWriter out = new PrintWriter(new File(filename), "UTF-8");
out.print(text);
out.close();
答案 2 :(得分:0)
你可以试试这个
InputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\sample.txt"));
Reader reader =
new InputStreamReader(inputStream, Charset.forName("UTF-8"));
答案 3 :(得分:0)
我不是专家,但找到了一个可以帮助您的链接:Converting a txt File from ANSI to UTF-8 programmatically
此处解释了与此相关的一些问题:http://www.drillio.com/en/software-development/java/encoded-string-too-long-64kb-limit/
我希望这会有所帮助。