Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a txt File from ANSI to UTF-8 programmatically

I need your help here please. I'm working on a java application that convert data from a txt file into the database , The problem is that the file have ANSI encoding which i can't change because it comes from outside my application ,and when i write the data to the database i got some "???" inside. My question is , how can i convert the data that i read from the file from ANSI to UTF-8 which can handle those weired symbols. I've tried the Byte[] to String converting but it didn't work.

like image 484
wlegend Avatar asked May 31 '11 20:05

wlegend


People also ask

How do I change ANSI File 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.


1 Answers

Use open a decoding Reader like this one:

Reader reader = 
   new InputStreamReader(inputStream, Charset.forName(encodingName));

Exaclty which encoding name you should use depends on which "ANSI" encoding the text file was written in. You can find a list of encoding supported by Java 6 here. If it is an English-language system, it will likely be windows-1252.

Writing data to the database correctly depends on configuring the database correctly and (sometimes) providing the right configuration to the JDBC driver.

You can read more about character encoding handling in here and here.

like image 116
McDowell Avatar answered Oct 13 '22 13:10

McDowell