首页 > 范文大全 > 正文

用Visual Studio 2005解决手机查看txt文件出现乱码的问题

开篇:润墨网以专业的文秘视角,为您筛选了一篇用Visual Studio 2005解决手机查看txt文件出现乱码的问题范文,如需获取更多写作素材,在线客服老师一对一协助。欢迎您的阅读与分享!

摘要:智能手机不仅仅可以用来打电话,还可以用来阅读从网上下载的书籍。现在网上的很多书籍采用的是txt格式。可是,有时会遇到在计算机中能正常显示的文本文件,在手机中却全是乱码,无法阅读。该文就针对这一问题用visual studio 2005编个简单的小程序,来帮助广大的手机用户解决txt格式的书籍出现乱码问题

关键词:Visual studio 2005;智能手机;编码;Unicode ANSI txt

中图分类号:TP311文献标识码:A文章编号:1009-3044(2010)03-718-01

The Application of Visual Studio 2005 to Solve the Messy Code Problem of TXT File from Mobile Phone

ZHANG Mei-Xin1, CUI Zhi-Yun2

(1.Forensic Science Lab, The College of Political Science and Law, Baoding 071002, China; 2.Office of Information and Network management, Command College of the Chinese Armed Police Forces, Tianjing 300000, China)

Abstract: Smartphone can be used not only to communicate, but also to read books downloaded from the Internet.?A lot of books downloaded from the internet are in text format.?Sometimes, the text files which can be properly displayed in computer are displayed abnormally in smart phone and unreadable, which is because of the difference of text default encoding format between computer and smartphone. A program is compiled in the environment of visual studio 2005 and windows XP to solve this problem.

Key words: visual studio 2005; smartphone; encoding; Unicode ANSI txt

现在的大多数智能手机都可以用来阅读txt格式文件的书籍,我们可以随时随地利用手机阅读自己喜欢的小说,这极大地丰富了我们的空闲时间。但是在利用手机阅读txt书籍的时候,会经常遇到下面这种情况,一些在计算机上能正常阅读的txt书籍,拷贝到手机上,用手机打开的时候却全是乱码,根本无法阅读。造成这种情况的原因,是因为手机自带的记事本程序只能正常阅读编码方式是Unicode的txt文件,而我们在计算机中上用来处理txt文件的常用工具,比如Windows自带的记事本、写字板,生成txt文件时的默认编码方式却是ANSI。当把编码为ANSI的txt文件拷贝到手机中,由于和手机能识别的Unicode编码方式不一致,就会造成在计算机上能正常显示的txt文件,在手机上显示的却是乱码。

知道了造成乱码的原因,解决起来就简单了。对于这些在手机上出现乱码的txt文件,把它们复制到计算机中,用写字板打开,然后选择另存为,保存类型选择Unicode文本文档。这样保存过的txt文件再用手机阅读的时候就不会出现乱码了。如果只是一个或者几个这样的文件,使用这种方式转换很方便。但是,如果有很多这种乱码文件,每个文件都要通过手工方式另存一遍不仅慢而且麻烦。为解决这个问题,我们可以编个简单的小程序,利用程序自动转换txt文件的编码格式,不管有多少这种文件,都可以一次转换过来。

打开visual studio 2005,建立一个console工程,在 Sub Main()中输入代码。

首先定义一个集合变量fileList,用来存放需要转换的文本文件名,然后定义一个字符串变量foundFile用来枚举集合fileList中的每一个文件,定义一个字符串变量stringReader存放从文本文件中读出的文件内容。

Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

Dim foundFile As String

Dim stringReader As String

从当前文件夹中,找出所有的扩展名为txt的文件,存放到fileList集合中。

fileList = puter.FileSystem.GetFiles(puter.FileSystem.CurrentDirectory, _FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

定义一个整型变量,用来记录转换了多少txt文件

Dim i As Integer

i = 0

对于fileList集合中的每一个文件,把文件内容读出来存放到字符串stringReader,读取文件内容时采用的编码方式是Default。

For Each foundFile In fileList

stringReader = puter.FileSystem.ReadAllText(foundFile, _System.Text.Encoding.Default)

把stringReader中存放的文件内容转换成unicode编码,以覆盖的方式重写到原来的文件中,写完后,相应的txt文件编码方式就变成了unicode编码。

puter.FileSystem.WriteAllText(foundFile, stringReader, False, _System.Text.Encoding.Unicode)用变量i记录转换了多少文件

i = i + 1

Next

利用信息框显示转换了多少文件

Msgbox(“成功转换了” & Cstr(i) &“个文件!”)

End Sub

把程序编译生成一个可执行程序,把这个可执行程序复制到需要转换编码的txt文件所在的文件夹中。运行该程序,该程序就可以把当前文件夹中的所有txt文件的编码方式都转换成unicode编码。然后把这些转换后的文本文件再复制到手机中,就可以用手机正常阅读这些txt文件了。(程序在visual studio 2005和Windows XP环境中编译通过)

参考文献:

[1] puter.FileSystem.GetFiles方法,/zh-cn/library/t71ykwhb(VS.80).aspx.

[2] puter.FileSystem.ReadAllText方法,/zh-cn/library/ks09bsaw(VS.80).aspx.

[3] puter.FileSystem.WriteAllText方法,/zh-cn/library/27t17sxs(VS.80).aspx.