把集合数据写入SD卡
2018-03-24 09:32
260 查看
加入读写权限<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
写的方法
//运行的时候把方法调用一下
public boolean writeListIntoSDcard(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir = Environment.getExternalStorageDirectory();//获取sd卡目录
//写入的文件名
File sdFile = new File(sdCardDir, "user.text");
try {
FileOutputStream fos = new FileOutputStream(sdFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//要写入的集合 (如果要写入的是对象直接把对象名字替换掉集合名字就行了)
oos.writeObject(list2);//写入
fos.close();
oos.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
else {
return false;
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
写的方法
//运行的时候把方法调用一下
public boolean writeListIntoSDcard(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir = Environment.getExternalStorageDirectory();//获取sd卡目录
//写入的文件名
File sdFile = new File(sdCardDir, "user.text");
try {
FileOutputStream fos = new FileOutputStream(sdFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//要写入的集合 (如果要写入的是对象直接把对象名字替换掉集合名字就行了)
oos.writeObject(list2);//写入
fos.close();
oos.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
else {
return false;
}
}
相关文章推荐
- Android获取assets文件夹中的数据并写入SD卡示例
- android读写sd卡操作写入数据读取数据示例
- android 写行为日志到SD卡 并发处理 异步写入数据到文件不影响界面响应时间
- Android应用程序对SD卡数据的写入和读取
- 将数据写入SD卡
- android 向SD卡写入数据
- 如何往外置sd卡中写入数据
- 如何把字符串写入SD卡文件中,如何读取SD卡文件的数据
- android 向SD卡写入数据
- android OutputStream向sd卡中文件写入操作立即关机数据清0问题
- Android中使用照相机权限、在SD卡创建和删除文件权限、在SD卡中写入数据权限
- Android编程实现向SD卡写入数据的方法
- 基于FATFS文件系统使用SDIO的4线模式在SD卡上写入数据(一)
- 关于android写入SD卡数据的学习代码
- 把集合对象写入sd卡
- 关于android写入SD卡数据的学习代码
- Android数据读取 写入操作(SD卡文件存储、SharedPreferences存储)
- 安卓系统:在SD卡上新建一个文件file.txt,并将经纬度和时间数据写入
- 将实体对象集合数据写入Excel的通用类
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]