当前位置: 首页 > 工具软件 > Text_storage > 使用案例 >

安卓 外部存储/ExternalStorage 的使用

昝卓
2023-12-01

权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

将数据写入外部存储设备

File directory = Environment.getExternalStorageDirectory();
File file = new File(directory+"/"+"text.txt");

// 将数据写入外部存储设备
try {
	FileOutputStream fileOutputStream = new FileOutputStream(file);
	fileOutputStream.write("666".getBytes());
	fileOutputStream.close();
     	
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();

} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

读取外部存储设备数据

// 读取外部存储设备数据
File directory1 = Environment.getExternalStorageDirectory();
File file1 = new File(directory1+"/"+"text.txt");

try {
	FileInputStream fileInputStream = new FileInputStream(file1);
	byte[] byte1 = new byte[fileInputStream.available()];
	if(fileInputStream.read(byte1)!= -1){
	String data =  new String(byte1);
	Log.w("data:", data);
}

} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
 类似资料: