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. SFTP文件上传下载

    http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html  (转载)

  2. 【Codeforces 158A】Next Round

    [链接] 我是链接,点我呀:) [题意] 让你找到排名的前k名,并列的话,如果分数大于0那么就算晋级 问你最后有多少人可以晋级. [题解] 按照题意模拟就好, 先按照a[max] = a[k]的规则找 ...

  3. OceanBase架构浅析(一)

    简介 OceanBase是阿里集团研发的可扩展的关系数据库,实现了数千亿条记录.数百TB数据上的跨行跨表事务,截止到2012年8月,支持了收藏夹.直通车报表.天猫评价等OLTP和OLAP在线业务,线上 ...

  4. 查看OS 各项参数

    查看CPU 在linux下 cat /proc/cpuinfo 可以得到CPU信息. 要注意的是CPU型号有不同的种类比如AMD Intel.可能在这个文件中显示的信息也不同.但终归是存在这个文件中的 ...

  5. MVC.Net:通过Global.asax捕捉错误

    在MVC.Net中,如果我们想做一个统一的错误处理的模块,有几个选择,一种是通过一个Base Controller来实现,另外一种就是在Global.asax中实现.这里介绍后一种方法. 首先打开Gl ...

  6. CF #324 DIV2 C题

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  7. IOS6.0自带下拉刷新控件UIRefreshControl

    1.UIRefreshControl必需要在IOS6.0以后才干使用,同一时候他仅仅能在UITableViewController类中才干够使用 2.使用比較简单 self.refreshContro ...

  8. C++ 句柄类的原理以及设计

    句柄类存在的意义是为了弥补将派生类对象赋给基类对象时发生的切片效应.比如以下的程序: multimap<Base> basket; Base base; Derived derive; b ...

  9. hook 鼠标键盘消息实例分析

    1.木马控制及通信方法包含:双管道,port重用.反弹技术.Hook技术,今天重点引用介绍一下hook的使用方法,hook信息后能够将结果发送到hacker邮箱等.实现攻击的目的. 转自:http:/ ...

  10. Spring+Mybatis之登录功能demo

    其实工作之后就没有用过Spring+Mybatis的框架了,因为公司有一个自己开发的框架,讲道理,其实这个与Spring+Mybatis整合很是神似.当然性能上还是比不上Spring+Mybatis所 ...