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;}}