Android:文件读写

3.10 Android读写文件

1、读写文件

    Android读写文件操作,不能写入到系统根目录,只能在应用包下文件夹进行读写。

    使用getCacheDir()方法,获取当前应用的Cache目录路径;

    使用getFilesDir()方法,获取当前应用的files目录路径;

示例:

//读取数据

public void readData(){

    try {

        //读取文件,参数文件路径:/data/data/包名/文件名

        File file = new File("路径");

        BufferedReader reader = new BufferedReader(new FileReader(file));

        //读取一行

        String line = reader.readLine();

    }catch (Exception e){

        e.printStackTrace();

    }

}

//写文件到指定目录

public void saveData1() {

    try {

        //文件对象,参数文件路径:/data/data/包名/文件名

        File file = new File("路径");

        //获取cache目录

        File file1 = new File(getCacheDir(), "filename");

        //获取files目录

        File file2 = new File(getFilesDir(), "filename");

        //字符流

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        writer.write("hehe");

        writer.close();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

2、手机存储与文件系统

内部存储:/data;

外部存储:外部SD卡;

示例:

//1获取内部存储/data

File filedata = Environment.getDataDirectory();

//2获取外部存储

File exfiledata = Environment.getExternalStorageDirectory();

Android内部文件系统

Android是Linux系统,文件权限操作和Linux类似。

文件权限:

1:-代表文件,d代表目录;

2~4:rwx,用户权限,读写执行;

5~7:rwx,组权限,读写执行;

8~10:rwx,其他权限;

1、一个应用代表一个用户;

  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是不器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值