读写sd卡文件,只需要用普通Java io就可以了,Environment.getExternalStorageDirectory()为sd卡路径的File
注意:用模拟器运行时需要加载sd卡支持,否则会报read-only system
一,写文件
try {
File myfile = new File(Environment.getExternalStorageDirectory(), "MyDemo.txt");
OutputStream os = new FileOutputStream(myfile);
os.write("hello".getBytes());
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
二,读文件
try{
File myfile = new File(Environment.getExternalStorageDirectory(), "MyDemo.txt");
InputStream file = new FileInputStream(myfile);
byte[] b = new byte[(int)myfile.length()];
file.read(b);
Log.d(TAG, new String(b));
}catch(Exception e){
e.printStackTrace();
}
本文介绍了如何使用Java IO操作来实现Android设备上SD卡文件的读取与写入功能。文中提供了具体的代码示例,包括创建文件、写入数据及读取文件内容等步骤,并提醒了在模拟器上进行测试时需要注意的事项。

1689

被折叠的 条评论
为什么被折叠?



