Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert ANSI to utf8 in java? [duplicate]

Tags:

java

unicode

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.

like image 819
PS Kumar Avatar asked Aug 09 '13 06:08

PS Kumar


People also ask

How do you convert ANSI to UTF-8?

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.

Is UTF-8 the same as ANSI?

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.

Does Java use UTF-8 or UTF-16?

The native character encoding of the Java programming language is UTF-16.


1 Answers

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());
like image 150
sgbj Avatar answered Sep 22 '22 04:09

sgbj