Android 读取assets文件下的txt文件
生活随笔
收集整理的這篇文章主要介紹了
Android 读取assets文件下的txt文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ndroid 讀取assets文件下的txt文件,解決了讀取txt文件的亂碼問題;
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package com.example.com.scrollview; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { private TextView novel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); novel = (TextView) findViewById(R.id.novel); AssetManager am=getAssets(); try { InputStream is=am.open("zx.txt"); String code=getCode(is); is=am.open("zx.txt"); BufferedReader br=new BufferedReader(new InputStreamReader(is,code)); String line=br.readLine(); int i=0; while(line!=null){ novel.append(line+"\n"); line=br.readLine(); i++; if(i==200){ break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // novel.setText(readNovel()); } public String getCode(InputStream is){ try { BufferedInputStream bin = new BufferedInputStream(is);? ???int p; p = (bin.read() << 8) + bin.read(); ?? ???String code = null;? ?????? ???switch (p) {? ???????case 0xefbb:? ???????????code = "UTF-8";? ???????????break;? ???????case 0xfffe:? ???????????code = "Unicode";? ???????????break;? ???????case 0xfeff:? ???????????code = "UTF-16BE";? ???????????break;? ???????default:? ???????????code = "GBK";? ???} ???is.close(); ???return code; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }?? return null; } } |
總結
以上是生活随笔為你收集整理的Android 读取assets文件下的txt文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android中TextView显示中文
- 下一篇: 一个简单的Android音乐播放器