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.
Try Settings -> Preferences -> New document -> Encoding -> choose UTF-8 without BOM, and check Apply to opened ANSI files . That way all the opened ANSI files will be treated as UTF-8 without BOM.
ANSI and UTF-8 are both encoding formats. ANSI is the common one byte format used to encode Latin alphabet; whereas, UTF-8 is a Unicode format of variable length (from 1 to 4 bytes) which can encode all possible characters.
The native character encoding of the Java programming language is UTF-16.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With