[转]Android读写文件
一、 从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读写文件的更多相关文章
- Android 读写文件
Android 读写文件 Android使用一个非常类似与其他平台上的基于磁盘的文件系统. 这节课讲述如何利用File APIs在Android文件系统中读写文件. File 对象非常适合于流式顺序数 ...
- Android -- 读写文件到内部ROM,SD卡,SharedPreferences,文件读写权限
(内容整理自张泽华教程) 1. 概述 使用文件进行数据存储 首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过 ...
- Android读写文件
1.从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = getResour ...
- android 学习随笔二(读写文件)
在android读写文件 RAM:运行内存,相当于电脑的内存 ROM:内部存储空间,相当电脑硬盘,android手机必须有的 SD卡:外部存储空间,相当电脑的移动硬盘,不是必须的.手机如果内置16G存 ...
- android 写文件权限
首先,在manifest.xml中添加user permission:<uses-permission android:name="android.permission.WRITE_E ...
- Android 在内部存储读写文件
文件读写操作* Ram内存:运行内存,相当于电脑的内存* Rom内存:内部存储空间,相当于电脑的硬盘* sd卡:外部存储空间,相当于电脑的移动硬盘在内部存储空间中读写文件>小案例:用户输入账号密 ...
- Android 读写SD卡的文件
今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...
- Android简易实战教程--第十五话《在外部存储中读写文件》
第七话里面介绍了在内部存储读写文件 点击打开链接. 这样有一个比较打的问题,假设系统内存不够用,杀本应用无法执行,或者本应用被用户卸载重新安装后.以前保存的用户名和密码都不会得到回显.所以,有必要注意 ...
- Android初级教程理论知识(第二章布局&读写文件)
常见布局 相对布局 RelativeLayout 组件默认左对齐.顶部对齐 设置组件在指定组件的右边 android:layout_toRightOf="@id/tv1" 设置在指 ...
随机推荐
- Posix-linux_route
route命令显示或者修改本地IP路由表. 语法: [plain] route [-CFvnee] route [-v] [-A family] add [-net|-host] ...
- apply 与arguments的用法
一个小练习: 用一个 函数来代替console.log()的功能 function log(a){ console.log.apply(null,arguments);//arguments 是传的实 ...
- Lenovo G40-30 Install win7
SATA Controller mode 两种硬盘模式,如果装XP应该要选第二种模式,兼容:据说ACHI比IDE模式性能好,开机能快点, 装Win7,两种模式都可以,只不过装Win7一般人都用ACHI ...
- GDB错误:Cannot find bounds of current function
http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...
- ssh端口映射,本地转发
应用场景: # HOSTA<-X->HOSTB 表示A,B两机器相互不可以访问, HOSTA<-->HOSTB 表示A,B两机器可以相互访问# 1.localhost< ...
- JAVA中JNI的简单使用
了解JNI:JAVA因其跨平台特性而受人们喜爱,也正因此,使得它和本机各种内部联系变得很少,所以JNI(Java Native Interface)就是用来解决JAVA本地操作的一种方式.JAVA通过 ...
- [转] java Class类
Class类(在java.lang包中,Instances of the class Classrepresent classes and interfaces in a running Javaap ...
- Java基础知识强化74:正则表达式之分割功能 (扩展练习)
1. 看程序写结果:(面试题考过) package cn.itcast_03; /* * 分割功能练习 */ public class RegexDemo2 { public static void ...
- NYOJ528 找球号(三)位运算
找球号(三) 时间限制:2000 ms | 内存限制:3000 KB 难度:2 描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都是 ...
- mwc config.h 中文注释
#ifndef CONFIG_H_ #define CONFIG_H_ /*************************************************************** ...