安卓 外部存储/ExternalStorage 的使用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


权限

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


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android