如何在Java中将ANSI转换为utf8? [英] how to convert ANSI to utf8 in java?

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

问题描述

我有一个文本文件,它是ANSI编码,我必须将其转换为UTF8编码.

I have a text file it is ANSI Encoding, i have to convert it into UTF8 encoding.

我的文本文件是这样的随机程序设计是研究数学程序设计的一个领域如何在不确定性下建模决策问题.例如,尽管在给定的时间点可能需要做出决定,基本信息可能要等到以后才能使用.

My text file is like this 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.

推荐答案

您可以使用java.nio.charset.Charset类(windows-1252是ANSI的专有名称)来明确显示:

You can be explicit with the java.nio.charset.Charset class (windows-1252 is the proper name for 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());
}

或者如果您喜欢=,也可以一行显示

Or in one line if you prefer =)

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());

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

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