您的位置:首页 > 其它

把集合数据写入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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: