【自用】Andoid读写简单文件,自用不解释

本文介绍了在Android应用中如何实现文件的保存和读取操作。通过使用Java代码示例详细解释了如何创建文件路径、利用FileOutputStream进行文件写入以及通过FileInputStream实现文件内容的读取,并对可能出现的IOException进行了妥善处理。
//保存
private void saveFile(String fileName, String content)
{

    File pathFile = new File(getFilesDir(), fileName);
    try
    {
        FileOutputStream fos = this.openFileOutput(fileName,Activity.MODE_PRIVATE);
        fos.write(content.getBytes());
        fos.flush();
        fos.close();

    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

//读取
private int loadFile(String fileName)
{
    int content = 6;
    File pathFile = new File(getFilesDir(), fileName);
    if (pathFile.exists())
    {
        try
        {
            FileInputStream fis = new FileInputStream(pathFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));

            String content = br.readLine();
            br.close();
            fis.close();
            content = Integer.parseInt(content);

        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }
    return content;
}

 

转载于:https://my.oschina.net/kilosnow/blog/674287

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值