一、       从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上

二、       从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();

}

三、       从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);

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

String fileName = "TEST.txt";

String message = "FFFFFFF11111FFFFF" ;

writeFileData(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();

}

}

五、       写, 读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;

}

六、       写, 读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

http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html

[转]Android读写文件的更多相关文章

  1. Android 读写文件

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

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

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

  3. Android读写文件

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

  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. 一个linux下socket编程的例子,client连server

    关于socket编程,以下文章写得比较好:http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html 1. accept()函 ...

  2. 线性表(gcc实现)

    线性结构: ①存在一个唯一的被称为“第一个”的数据元素: ②存在一个唯一的被称为“最后一个”的数据元素: ③除第一个元素外,每个元素均有唯一一个直接前驱: ④除最后一个元素外,每个元素均有唯一一个直接 ...

  3. 使用 Make 命令构建网站

    网站开发正变得越来越专业,涉及到各种各样的工具和流程,迫切需要构建自动化. 所谓”构建自动化”,就是指使用构建工具,自动实现”从源码到网页”的开发流程.这有利于提高开发效率.改善代码质量. 本文介绍如 ...

  4. F - Warm up - hdu 4612(缩点+求树的直径)

    题意:有一个无向连通图,现在问添加一条边后最少还有几个桥 分析:先把图缩点,然后重构图为一棵树,求出来树的直径即可,不过注意会有重边,构树的时候注意一下 *********************** ...

  5. sql server 2005中IMAGE类型的BUG问题

    目的:在sql server 2005数据库上筛选出那些有照片的员工 由于客户之前的数据库是sql server 2000,定义的photo字段的数据类型为image, 在sql 2005数据库上,用 ...

  6. 一般处理程序中使用Session出现未将对象引用设置到对象的实例

    遇到问题:未将对象引用设置到对象的实例 那就在你的一般处理程序中加入红色背景的代码吧 using System; using System.Collections.Generic; using Sys ...

  7. 【bzoj3943】[Usaco2015 Feb]SuperBull

    题目描述 Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer Jo ...

  8. 沧海一声笑,移动应用的CRASH原因我找到! --记最新款数字化測试“星云測试“的使用攻略

    沧海一声笑,移动应用的CRASH原因我找到! --记最新款数字化測试"星云測试"的使用攻略 世界进步那么快,非常多新奇的点子层出不穷,于是我们创业.我们做最酷的手机应用,做最轰炸的 ...

  9. linux命令详解--tcpdump

    工作中一直在用tcpdump,感觉非常方便,今天心血来潮百度了一下tcpdump的用法,才发现原来还有这么多强大的功能自己都不知道,那叫一个汗啊. 以此文作为备份,记录一些新知道的用法,各位网友谁有新 ...

  10. Win32 的dll导入

    dll 文件可以导入变量,函数,和C++类,但是导入变量会使执行程序与dll紧耦合,而C++类导入则需要两个文件的开发商所用的编译器相兼容,所以做好只导入函数; 创建dll : 头文件:#ifdef  ...