1、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)

String res = ""; 

try{
InputStream in = getResources().openRawResource(R.raw.bbi);
//在\Test\res\raw\bbi.txt,
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
//res = EncodingUtils.getString(buffer, "UTF-8");
//res = EncodingUtils.getString(buffer, "UNICODE");
res = EncodingUtils.getString(buffer, "BIG5");
//依bbi.txt的编码类型选择合适的编码,如果不调整会乱码
in.close();
}catch(Exception e){
e.printStackTrace();
}
myTextView.setText(res);//把得到的内容显示在TextView上

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

String fileName = "yan.txt"; //文件名字
String res="";
try{
InputStream in = getResources().getAssets().open(fileName);
// \Test\assets\yan.txt这里有这样的文件存在
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}

3、      
从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:/

String fileName = "/sdcard/Y.txt";
//也可以用String fileName = "mnt/sdcard/Y.txt";
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
//FileInputStream fin = openFileInput(fileName);
//用这个就不行了,必须用FileInputStream
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}catch(Exception e){
e.printStackTrace();
} myTextView.setText(res);

3、写文件,
一般写在\data\data\com.test\files\里面,

打开DDMS查看file
explorer是可以看到仿真器文件存放目录的结构的

   String fileName = "TEST.txt";
String message = "FFFFFFF11111FFFFF" ;
<u>writeFileData</u>(fileName, message);
public voidwriteFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}

5、  写、读data/data/目录(相当AP工作目录)上的文件

用openFileOutput

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

   public voidwriteFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
} catch(Exception e){
e.printStackTrace();
}
} //------------------------------------------------------- //读文件在./data/data/com.tt/files/下面 public String readFileData(String fileName){
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res; }

6、     写、读sdcard目录上的文件

要用FileOutputStream,
不能用openFileOutput

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

   public voidwriteFileSdcard(String fileName,String message){
try{
//FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
FileOutputStream fout = newFileOutputStream(fileName);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
} catch(Exception e){
e.printStackTrace();
}
} //读在/mnt/sdcard/目录下面的文件 public String readFileSdcard(String fileName){
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
} catch(Exception e){
e.printStackTrace();
}
return res; }

注:
openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以

参考:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6017.html

Android读写文件的更多相关文章

  1. Android 读写文件

    Android 读写文件 Android使用一个非常类似与其他平台上的基于磁盘的文件系统. 这节课讲述如何利用File APIs在Android文件系统中读写文件. File 对象非常适合于流式顺序数 ...

  2. Android -- 读写文件到内部ROM,SD卡,SharedPreferences,文件读写权限

    (内容整理自张泽华教程) 1. 概述 使用文件进行数据存储 首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过 ...

  3. [转]Android读写文件

    一.       从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = ge ...

  4. android 学习随笔二(读写文件)

    在android读写文件 RAM:运行内存,相当于电脑的内存 ROM:内部存储空间,相当电脑硬盘,android手机必须有的 SD卡:外部存储空间,相当电脑的移动硬盘,不是必须的.手机如果内置16G存 ...

  5. android 写文件权限

    首先,在manifest.xml中添加user permission:<uses-permission android:name="android.permission.WRITE_E ...

  6. Android 在内部存储读写文件

    文件读写操作* Ram内存:运行内存,相当于电脑的内存* Rom内存:内部存储空间,相当于电脑的硬盘* sd卡:外部存储空间,相当于电脑的移动硬盘在内部存储空间中读写文件>小案例:用户输入账号密 ...

  7. Android 读写SD卡的文件

    今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...

  8. Android简易实战教程--第十五话《在外部存储中读写文件》

    第七话里面介绍了在内部存储读写文件 点击打开链接. 这样有一个比较打的问题,假设系统内存不够用,杀本应用无法执行,或者本应用被用户卸载重新安装后.以前保存的用户名和密码都不会得到回显.所以,有必要注意 ...

  9. Android初级教程理论知识(第二章布局&读写文件)

    常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ...

随机推荐

  1. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

  2. [bzoj3669][Noi2014]魔法森林_LCT_并查集

    魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...

  3. Linq:Linq实例1..More

    本文会不断更新应用实例. 需求1:对一个Rate列表的RateLevel属性求和,然后除以Rate列表的数量求平均值. 没有Linq的做法: Int rateLevel = ; foreach (Ra ...

  4. php session自定义处理

    原文:http://www.cnblogs.com/mrcoke/  这个人的博客上转的. 这个博客也好: 学算法和数据结构!!http://blog.csdn.net/21aspnet/articl ...

  5. python 使用scapy编写DNS Fuzzer

    1. 描写叙述 使用scapy库,编写一个DNS Fuzzer工具,并測试.在这之前.先说明一下DNS协议请求包是封装在IP包中的UDP包(有些情况也可使用TCP)中.且UDP的端口为53. 进入sc ...

  6. 【Java集合源代码剖析】Hashtable源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/36191279 Hashtable简单介绍 Hashtable相同是基于哈希表实现的,相同每 ...

  7. Vim技巧之四大模式_普通模式

    Vim技巧之四大模式_普通模式 一见不钟情的普通模式 普通模式以下的强悍操作 什么是操作符 什么是动作命令 误操作怎么办 那种操作更划算 普通模式下的神奇大招 Vim技巧之四大模式_普通模式 众所周知 ...

  8. Coder-Strike 2014

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove? viewmode=contents    by---cxlove Qualification Round 略 Ro ...

  9. poj 1741(树的点分治)

    Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  10. gdb 断点调试C程序

    最近在看CS50的公开课,视频中david用gdb调试C,我跟着敲,一样的代码但是却显示效果与他不一样.因为他的程序是编译好了的,所以也没看到编译步骤,后来回想一下他make 文件名 显示的代码中有一 ...