将txt文件从ANSI转换为UTF-8,以编程方式 [英] Converting a txt File from ANSI to UTF-8 programmatically
问题描述
我正在研究将数据从txt文件转换成数据库的java应用程序,问题是文件有ANSI编码,我不能改变,因为它来自我的应用程序外面,当我写数据库中的数据我有一些???内。
我的问题是,如何将我从文件中读取的数据从ANSI转换为可以处理这些被排除的符号的UTF-8。
我已经尝试过Byte []到String转换,但没有起作用。
解读Reader 如下所示:
Reader reader =
new InputStreamReader(inputStream,Charset.forName (encodingName));
您应该使用哪个编码名称的Exaclty取决于其中编写了文本文件的ANSI编码。您可以找到Java 6支持的编码列表here 。如果它是一个英文系统,它可能是 windows-1252
。
将数据写入数据库正确地依赖于正确配置数据库(有时)为JDBC驱动程序提供正确的配置。
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.
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.
这篇关于将txt文件从ANSI转换为UTF-8,以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!