关于android里的文件创建及读写问题
2016-05-03 16:54
441 查看
一 在SD卡创建并读写文件
SD卡正常加载后,通过Eclipse的DDMS的File explorer可以看到 mnt/sdcard的权限属性为d---rwxr-x,很奇怪的属性,sdcard的owner是system用户,但system用户对sdcard没有读写可执行权限,所以如果需要对sdcard操作,就不能是system用户。
读写SD卡需要在app的manifest中添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />申请权限
然后在java文件里
File newxmlfile = new File(Environment.getExternalStorageDirectory(),"aaa.xml");
try{
if(!newxmlfile.exists())
newxmlfile.createNewFile();
}catch(IOException e){
Log.e("IOException", "exception in createNewFile() method");
return;
}
//we have to bind the new file with a FileOutputStream
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
return;
}
上面是创建并写文件,如果是读就用FileInputStream
二 在data目录创建并读写文件
android的应用如果想在data目录下创建文件,只能用android提供的方法
FileOutputStream fileos = null;
try{
fileos=context.openFileOutput("aaa.xml",Context.MODE_WORLD_READABLE);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
return;
}
其中context是该android应用的ApplicationContext,上面这种方法会查看/data/data/mypackage/files目录下是否有aaa.xml文件,如果没有,则会自动创建(files目录也是自动创建的),Context.MODE_WORLD_READABLE是参数,代表aaa.xml可以被其他应用读。共有四种参数,这里不做介绍了。
如果app想在data的其他目录下创建文件,是不会成功的。比如在/data根目录下创建文件,会抛出异常,但可以读/data目录下的文件,也可以对/data下文件修改。
如果想读文件,有两种方法
方法一(android提供的方法)
FileInputStream fis= null;
try{
fis=context.openFileInput("aaa.xml");
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "Couldn't find or open policy file");
return;
}
这种方法读的是/data/data/mypackage/files目录下的aaa.xml
方法二(普通方法)
File policyFile = new File("/data/aaa.xml");
FileInputStream fis=null;
try {
fis = new FileInputStream(policyFile);
} catch (FileNotFoundException e) {
Log.w(TAG, "Couldn't find or open policy file " + policyFile);
return null;
}
SD卡正常加载后,通过Eclipse的DDMS的File explorer可以看到 mnt/sdcard的权限属性为d---rwxr-x,很奇怪的属性,sdcard的owner是system用户,但system用户对sdcard没有读写可执行权限,所以如果需要对sdcard操作,就不能是system用户。
读写SD卡需要在app的manifest中添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />申请权限
然后在java文件里
File newxmlfile = new File(Environment.getExternalStorageDirectory(),"aaa.xml");
try{
if(!newxmlfile.exists())
newxmlfile.createNewFile();
}catch(IOException e){
Log.e("IOException", "exception in createNewFile() method");
return;
}
//we have to bind the new file with a FileOutputStream
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
return;
}
上面是创建并写文件,如果是读就用FileInputStream
二 在data目录创建并读写文件
android的应用如果想在data目录下创建文件,只能用android提供的方法
FileOutputStream fileos = null;
try{
fileos=context.openFileOutput("aaa.xml",Context.MODE_WORLD_READABLE);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
return;
}
其中context是该android应用的ApplicationContext,上面这种方法会查看/data/data/mypackage/files目录下是否有aaa.xml文件,如果没有,则会自动创建(files目录也是自动创建的),Context.MODE_WORLD_READABLE是参数,代表aaa.xml可以被其他应用读。共有四种参数,这里不做介绍了。
如果app想在data的其他目录下创建文件,是不会成功的。比如在/data根目录下创建文件,会抛出异常,但可以读/data目录下的文件,也可以对/data下文件修改。
如果想读文件,有两种方法
方法一(android提供的方法)
FileInputStream fis= null;
try{
fis=context.openFileInput("aaa.xml");
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "Couldn't find or open policy file");
return;
}
这种方法读的是/data/data/mypackage/files目录下的aaa.xml
方法二(普通方法)
File policyFile = new File("/data/aaa.xml");
FileInputStream fis=null;
try {
fis = new FileInputStream(policyFile);
} catch (FileNotFoundException e) {
Log.w(TAG, "Couldn't find or open policy file " + policyFile);
return null;
}
相关文章推荐
- Android创建文件夹及文件并写入数据
- android图表——MPAndroidchart
- android学习,在按钮或者文字上添加图片
- Android计算缓存大小、清空缓存
- Android 实现答题器功能(通过手势实现翻页效果)
- Android GridView Item居中(可能是最完美的解决方案)
- Android 之 OO Principle(面向对象的原则)
- Android Loader详解
- android 判断程序是首次(第一次)进入
- Android中的进程与线程
- Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 透明状态栏了解使用 及SystemBarTint 解读
- Android开发:相册读取、拍照、图片裁剪和图片上传服务器等功能的实现
- Android之自定义View的实现
- android开发步步为营之101:动态调整view的宽度和高度以及在页面的坐标位置
- Android Studio 生成APK实践
- Android Studio如何用release签名进行debug调试
- android Button防止多次点击
- Android线程中设置控件
- android view用动画实现遮罩效果