首页 > 其他分享 >安卓 外部存储/ExternalStorage 的使用

安卓 外部存储/ExternalStorage 的使用

时间:2023-03-14 12:35:04浏览次数:46  
标签:存储 安卓 printStackTrace File 存储设备 catch new ExternalStorage data


权限

<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();
}


标签:存储,安卓,printStackTrace,File,存储设备,catch,new,ExternalStorage,data
From: https://blog.51cto.com/u_16006123/6120313

相关文章