Android-第十三节01文件存储详解

FileOutputStream fileOutputStream = null;

try {

String text = “hello world”;

fileOutputStream = openFileOutput(“test.txt”, MODE_PRIVATE);

fileOutputStream.write(text.getBytes());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.运行后,会发现data/data/包名目录下多了一个文件

在这里插入图片描述

openFileOutput方法参数详解

@Override

public FileOutputStream openFileOutput(String name, int mode)

throws FileNotFoundException {

}

  • name 存储文件名字

  • mode 存储方式 其值的含义如下

在这里插入图片描述

方法二 传统方法读写

1.指定路径,再往里面写数据

在这里插入图片描述

2.核心代码

String text = “方法二传统方法读写”;//文件内容

File file1=new File(“/data/data/com.example.a_test13/files”,“test4.txt”);//指定路径

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream(file1);

fileOutputStream.write(text.getBytes());

} catch (Exception e) {

e.printStackTrace();

}finally {

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.2.2读数据

法一 openFileInput

1.核心代码:

FileInputStream fileInputStream = null;

Reader reader = null;

BufferedReader bufferedReader = null;

try {

fileInputStream = openFileInput(“test2.txt”);

reader = new InputStreamReader(fileInputStream);// 字符流

bufferedReader = new BufferedReader(reader); //缓冲流

StringBuilder result = new StringBuilder();

String temp;

while ((temp = bufferedReader.readLine()) != null) {

result.append(temp);

}

Log.i(“MainActivity”, “result:” + result);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fileInputStream != null) {

try {

fileInputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.运行结果

在这里插入图片描述

方法二 到指定文件夹下读取

1.核心代码

File file=new File(“/data/data/com.example.a_test13/files”, “test4.txt”);

inputStream = new FileInputStream(file);

reader = new InputStreamReader(inputStream);

bufferedReader = new BufferedReader(reader);

StringBuilder result = new StringBuilder();

String temp;

while ((temp = bufferedReader.readLine()) != null) {

result.append(temp);

}

Log.i(“MainActivity”, “result:” + result);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.运行结果

在这里插入图片描述

2.3sdcard目录


sdcard读写的数据将数据保存在 /mnt/sdcard目录

在这里插入图片描述

2.3.1动态权限

如果是对sd卡进行读写,必须要申请动态权限

首先,我们要先到mainifests里面添加权限

如果是 AndroidQ 或者更高的版本还需要在 manifest.xml 文件中的 application 节点下加入

android:requestLegacyExternalStorage=“true”

在这里插入图片描述

onCreate方法里面调用

checkPermission();

private static String[] PERMISSIONS_STORAGE = {“android.permission.READ_EXTERNAL_STORAGE”,

“android.permission.WRITE_EXTERNAL_STORAGE”};

private void checkPermission() {

//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission

.WRITE_EXTERNAL_STORAGE)) {

Toast.makeText(this, “请开通相关权限,否则无法正常使用本应用!”,

Toast.LENGTH_SHORT).show();

}

//申请权限

ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);

} else {

Toast.makeText(this, “已授权成功!”, Toast.LENGTH_SHORT).show();

}

}

@Override

public void onRequestPermissionsResult(int requestCode,

String permissions[], int[] grantResults) {

switch (requestCode) {

case REQUEST_EXTERNAL_STORAGE: {

if (grantResults.length > 0

&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Toast.makeText(this, “授权成功!”, Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, “授权被拒绝!”, Toast.LENGTH_SHORT).show();

}

return;

}

}

}

在这里插入图片描述

补充:为什么读取SD卡需要动态申请权限呢? 是因为在sd卡里面有很多我们个人隐私的文件,比如相册,如果允许权限了,那么就可以读到我们相册里面的所有照片,比如还有读地理位置,读短信,所以在Android6.0以后,对于这一类带有“危险”操作的权限,是需要动态申请权限的,由用户来选择是否允许

2.3.2写数据

添加 writeSdcard() 方法代码

private void writeSdcard() {

String text = “hello world”;

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

File storage = Environment.getExternalStorageDirectory();

File tmepfile = new File(storage.getPath());

if (! tmepfile.exists()) {

tmepfile.mkdirs();

}

File file1=new File(tmepfile,“sdtest.txt”);

if (!file1.exists()){

try {

file1.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream(file1);

fileOutputStream.write(text.getBytes());

} catch (Exception e) {

e.printStackTrace();

}finally {

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}

运行结果:

在这里插入图片描述

2.3.3读数据

添加readSdcard()方法代码

private void readSdcard() {

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

InputStream inputStream = null;

Reader reader = null;

BufferedReader bufferedReader = null;

try {

File storage = Environment.getExternalStorageDirectory();

File tmepfile = new File(storage.getPath());

File file=new File(tmepfile, “sdtest.txt”);

inputStream = new FileInputStream(file);

reader = new InputStreamReader(inputStream);

bufferedReader = new BufferedReader(reader);

StringBuilder result = new StringBuilder();

String temp;

while ((temp = bufferedReader.readLine()) != null) {

result.append(temp);

}

Log.i(“MainActivity”, “result:” + result);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}

在这里插入图片描述

  • 29
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值