如何在java中将ANSI转换为utf8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18141162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert ANSI to utf8 in java?
提问by PS Kumar
I have a text file it is ANSI Encoding, i have to convert it into UTF8 encoding.
我有一个文本文件,它是 ANSI 编码,我必须将其转换为 UTF8 编码。
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.
我的文本文件是这样的
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.
回答by Lake
ASCII character subset maps to the same character encoding in UTF8, so the file does not really need any conversion.
ASCII 字符子集映射到 UTF8 中相同的字符编码,因此该文件实际上不需要任何转换。
To output a file in UTF-8, you can use:
要以 UTF-8 格式输出文件,您可以使用:
PrintWriter out = new PrintWriter(new File(filename), "UTF-8");
out.print(text);
out.close();
回答by Ruchira Gayan Ranaweera
You can try this
你可以试试这个
InputStream inputStream = new BufferedInputStream(new FileInputStream("D:\sample.txt"));
Reader reader =
new InputStreamReader(inputStream, Charset.forName("UTF-8"));
回答by DarkKnight
I am not an expert but found a link that can help you out here: Converting a txt File from ANSI to UTF-8 programmatically
我不是专家,但在这里找到了一个可以帮助您的链接:Converting a txt File from ANSI to UTF-8 programmatically
There were some issues related to this that are explained here: http://www.drillio.com/en/software-development/java/encoded-string-too-long-64kb-limit/
这里解释了一些与此相关的问题:http: //www.drillio.com/en/software-development/java/encoded-string-too-long-64kb-limit/
I hope this helps.
我希望这有帮助。
回答by sgbj
You can be explicit with the java.nio.charset.Charset class (windows-1252 is the proper name for ANSI):
您可以使用 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());
}
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());