在内部存储中读写文件

来源:互联网 发布:网络小胖表情包 编辑:程序博客网 时间:2024/06/07 13:14

一、写文件

<span style="white-space:pre"></span>//data/data/com.xxx:这就是内部存储空间的路径    File file = new File("data/data/com.xxx/info.txt");    FileOutputStream fos;try {fos = new FileOutputStream(file);fos.write((name + "##" + pass).getBytes());fos.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}

二、读文件

    File file = new File("data/data/com.itheima.rwinrom/info.txt");    if(file.exists()){    try {FileInputStream fis = new FileInputStream(file);//把字节流转换成字符流BufferedReader br = new BufferedReader(new InputStreamReader(fis));//读取txt文件里的用户名和密码String text = br.readLine();String[] s = text.split("##");    et_name.setText(s[0]);    et_pass.setText(s[1]);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}    }

三、通过api来获得文件路径

    //返回一个File对象,其路径是data/data/com.xxx/files//    File file = new File(getFilesDir(), "info.txt");        //返回值也是一个File对象,其路径是data/data/com.xxx/cache    File file = new File(getCacheDir(), "info.txt");


0 0
原创粉丝点击