首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android 讀取.txt资料出現亂碼的解決方案

2012-09-02 
Android 讀取.txt文件出現亂碼的解決方案其實就一句話:保證你文件的編碼方式和解碼方式一致就可以了。如果

Android 讀取.txt文件出現亂碼的解決方案
其實就一句話:保證你文件的編碼方式和解碼方式一致就可以了。

如果你僅僅是讀取外部的文件,那麼接下來這麼用:

public static String encodin = "UTF8"public static String encodout = "UTF8"static void writeOutput(String str) {try {                FileOutputStream fos = new FileOutputStream("test.txt");               Writer out = new OutputStreamWriter(fos, encodout);              out.write(str);                 out.close();} catch (IOException e) {e.printStackTrace();}}static String readInput() {StringBuffer buffer = new StringBuffer();try {                       FileInputStream fis = new FileInputStream("test.txt");                  InputStreamReader isr = new InputStreamReader(fis, encodin);                       Reader in = new BufferedReader(isr);                   int ch;                 while ((ch = in.read()) > -1) {                        buffer.append((char)ch);                      }in.close();return buffer.toString();} catch (IOException e) {e.printStackTrace();return null;}}

热点排行