摘要:智能手機不僅僅可以用來打電話,還可以用來閱讀從網上下載的書籍?,F在網上的很多書籍采用的是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 = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, _FileIO.SearchOption.SearchTopLevelOnly, \"*.txt\")
定義一個整型變量,用來記錄轉換了多少txt文件
Dim i As Integer
i = 0
對于fileList集合中的每一個文件,把文件內容讀出來存放到字符串stringReader,讀取文件內容時采用的編碼方式是Default。
For Each foundFile In fileList
stringReader = My.Computer.FileSystem.ReadAllText(foundFile, _System.Text.Encoding.Default)
把stringReader中存放的文件內容轉換成unicode編碼,以覆蓋的方式重寫到原來的文件中,寫完后,相應的txt文件編碼方式就變成了unicode編碼。
My.Computer.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] My.Computer.FileSystem.GetFiles方法,http://msdn.microsoft.com/zh-cn/library/t71ykwhb(VS.80).aspx.
[2] My.Computer.FileSystem.ReadAllText方法,http://msdn.microsoft.com/zh-cn/library/ks09bsaw(VS.80).aspx.
[3] My.Computer.FileSystem.WriteAllText方法,http://msdn.microsoft.com/zh-cn/library/27t17sxs(VS.80).aspx.