A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© y1787257661 中级黑马   /  2015-1-6 10:43  /  1061 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. String res = "";

  2. try{

  3. InputStream in = getResources().openRawResource(R.raw.bbi);

  4. //在\Test\res\raw\bbi.txt,

  5.    int length = in.available();      

  6.    byte [] buffer = new byte[length];        

  7.    in.read(buffer);         

  8.    //res = EncodingUtils.getString(buffer, "UTF-8");

  9.    //res = EncodingUtils.getString(buffer, "UNICODE");

  10.    res = EncodingUtils.getString(buffer, "BIG5");

  11.    //依bbi.txt的编码类型选择合适的编码,如果不调整会乱码

  12.    in.close();            

  13.    }catch(Exception e){

  14.       e.printStackTrace();         

  15.    }

  16. myTextView.setText(res);//把得到的内容显示在TextView上

  17.   

  18. 二、       从asset中获取文件并读取数据(资源文件只能读不能写)

  19. String fileName = "yan.txt"; //文件名字

  20. String res="";

  21. try{

  22.    InputStream in = getResources().getAssets().open(fileName);

  23.    // \Test\assets\yan.txt这里有这样的文件存在

  24.    int length = in.available();         

  25. byte [] buffer = new byte[length];        

  26. in.read(buffer);            

  27. res = EncodingUtils.getString(buffer, "UTF-8");     

  28. }catch(Exception e){

  29.       e.printStackTrace();         

  30.    }

  31.   

  32. 三、       从sdcard中去读文件,首先要把文件通过\android-sdk-windows\tools\adb.exe把本地计算机上的文件copy到sdcard上去,adb.exe push e:/Y.txt /sdcard/, 不可以用adb.exe push e:\Y.txt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull ./data/data/com.tt/files/Test.txt e:/

  33.   

  34. String fileName = "/sdcard/Y.txt";

  35. //也可以用String fileName = "mnt/sdcard/Y.txt";

  36. String res="";     

  37. try{

  38. FileInputStream fin = new FileInputStream(fileName);

  39. //FileInputStream fin = openFileInput(fileName);  

  40. //用这个就不行了,必须用FileInputStream

  41.     int length = fin.available();

  42.     byte [] buffer = new byte[length];

  43.     fin.read(buffer);     

  44.     res = EncodingUtils.getString(buffer, "UTF-8");

  45.     fin.close();     

  46.     }catch(Exception e){

  47.            e.printStackTrace();

  48. }

  49. myTextView.setText(res);

  50.   

  51. 四、       写文件, 一般写在\data\data\com.test\files\里面,打开DDMS查看file explorer是可以看到仿真器文件存放目录的结构的

  52.    String fileName = "TEST.txt";

  53.    String message = "FFFFFFF11111FFFFF" ;

  54. writeFileData(fileName, message);

  55.    

  56.    public voidwriteFileData(String fileName,String message){

  57.        try{

  58.         FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);

  59.         byte [] bytes = message.getBytes();

  60.         fout.write(bytes);

  61.          fout.close();

  62.         }

  63.        catch(Exception e){

  64.         e.printStackTrace();

  65.        }

  66.    }   

  67.   

  68. 五、       写, 读data/data/目录(相当AP工作目录)上的文件,用openFileOutput

  69.    //写文件在./data/data/com.tt/files/下面

  70.    public voidwriteFileData(String fileName,String message){

  71.        try{

  72.         FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);

  73.         byte [] bytes = message.getBytes();

  74.         fout.write(bytes);

  75.          fout.close();

  76.         }

  77.        catch(Exception e){

  78.         e.printStackTrace();

  79.        }

  80.    }

  81. //-------------------------------------------------------

  82. //读文件在./data/data/com.tt/files/下面

  83.    public String readFileData(String fileName){

  84.         String res="";

  85.         try{

  86.          FileInputStream fin = openFileInput(fileName);

  87.          int length = fin.available();

  88.          byte [] buffer = new byte[length];

  89.          fin.read(buffer);     

  90.          res = EncodingUtils.getString(buffer, "UTF-8");

  91.          fin.close();     

  92.         }

  93.         catch(Exception e){

  94.          e.printStackTrace();

  95.         }

  96.         return res;

  97.     }   

  98. 六、       写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput

  99.   

  100.     //写在/mnt/sdcard/目录下面的文件

  101.    public voidwriteFileSdcard(String fileName,String message){

  102.        try{

  103.         //FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);

  104.        FileOutputStream fout = newFileOutputStream(fileName);

  105.         byte [] bytes = message.getBytes();

  106.         fout.write(bytes);

  107.          fout.close();

  108.         }

  109.        catch(Exception e){

  110.         e.printStackTrace();

  111.        }

  112.    }

  113.    

  114.    //读在/mnt/sdcard/目录下面的文件

  115.    public String readFileSdcard(String fileName){

  116.         String res="";

  117.         try{

  118.          FileInputStream fin = new FileInputStream(fileName);

  119.          int length = fin.available();

  120.          byte [] buffer = new byte[length];

  121.          fin.read(buffer);     

  122.          res = EncodingUtils.getString(buffer, "UTF-8");

  123.          fin.close();     

  124.         }

  125.         catch(Exception e){

  126.          e.printStackTrace();

  127.         }

  128.         return res;

  129.    }
复制代码
您需要登录后才可以回帖 登录 | 加入黑马