以编程方式将 txt 文件从 ANSI 转换为 UTF-8 [英] Converting a txt File from ANSI to UTF-8 programmatically

查看:37
本文介绍了以编程方式将 txt 文件从 ANSI 转换为 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助.我正在开发一个将数据从 txt 文件转换为数据库的 Java 应用程序,问题是该文件具有 ANSI 编码,我无法更改,因为它来自我的应用程序外部,当我将数据写入数据库我得到了一些???"里面.我的问题是,如何将从文件中读取的数据从 ANSI 转换为可以处理那些 weired 符号的 UTF-8.我试过 Byte[] 到 String 的转换,但没有用.

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.

推荐答案

使用打开一个 像这样解码阅读器:

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

您应该使用哪种编码名称取决于哪种ANSI"编码文本文件是编写的中.您可以在此处 中找到Java 6 支持的编码列表.一>.如果是英文系统,很可能是windows-1252.

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.

正确地将数据写入数据库取决于正确配置数据库和(有时)为 JDBC 驱动程序提供正确的配置.

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

您可以在 此处阅读有关字符编码处理的更多信息此处.

这篇关于以编程方式将 txt 文件从 ANSI 转换为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆