读文件 Environment.getExternalStorageDirectory()

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

File file = new File(path);
if (file.exists()){
    try {
        BufferedReader bfr = new BufferedReader(new FileReader(path));
        String line = bfr.readLine();
        while (line != null) {
            line = bfr.readLine();
        }
        bfr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

package com.example.myapplication; import android.content.Context; import android.os.Build; import android.os.Environment; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FileSaveQQ { public static boolean saveUserInfo(Context context, String account, String password) { FileOutputStream fos = null; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { String SDPath ; if (Build.VERSION.SDK_INT > 29) { SDPath = context.getExternalFilesDir(null).getAbsolutePath(); } else { SDPath = Environment.getExternalStorageDirectory().getPath(); } File file = new File(SDPath, "data.txt"); try { fos = new FileOutputStream(file); fos.write((account + ":" + password).getBytes()); //将数据转换为字节码的形式写入data.txt文件中 fos.close(); } catch (Exception e) { e.printStackTrace(); } } return true; } //从data.txt文件中获取存储的QQ账号和密码 public static Map<String, String> getUserInfo(Context context) { String content = ""; FileInputStream fis = null; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { String SDPath ; if (Build.VERSION.SDK_INT > 29) { SDPath = context.getExternalFilesDir(null).getAbsolutePath(); } else { SDPath = Environment.getExternalStorageDirectory().getPath(); } File file = new File(SDPath, "data.txt"); try { fis = new FileInputStream(file); //获取文件的输入流对象fis byte[] buffer = new byte[fis.available()]; fis.read(buffer);//通过read()方法取字节码中的数据
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值