安卓SD卡输入输出
23 March 2013
1、首先AndroidManifest.xml里面要写权限,如下:
<!-- 在SDCard中创建与删除文件权限 -->
<uses -permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses>
<!-- 往SDCard写入数据权限 -->
<uses -permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses>
或
<uses -sdk android:minSdkVersion="3"></uses>
// 写文件
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile(); // 创建文件
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(i); // 填内容
myOutWriter.close();
fOut.close();
Toast.makeText(SongsActivity.this, "写入文件成功", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(SongsActivity.this, "写入文件失败" + e, Toast.LENGTH_SHORT)
.show();
}
// 读文件
File file = new File("/sdcard/mysdfile.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
String cc = new String(b);
Toast.makeText(SongsActivity.this, "读取文件成功", Toast.LENGTH_LONG)
.show();
getListView().setDividerHeight(Integer.parseInt(cc));
} catch (Exception e) {
Toast.makeText(SongsActivity.this, "读取失败" + e, Toast.LENGTH_SHORT)
.show();
}
blog comments powered by Disqus