Converting a txt File from ANSI to UTF-8 programma

2019-01-20 08:44发布

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.

1条回答
聊天终结者
2楼-- · 2019-01-20 09:30

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.

查看更多
登录 后发表回答