如何读写android 文件

来源:互联网 发布:域名加空间多少钱 编辑:程序博客网 时间:2024/05/16 07:55

读取文件 

  FileInputStream fis = openFileInput("first.txt"); //读取文件内容 first.txt 为要读取的文件路径,我这里是直接使用的android默认存储路径,所以直接写文件名。
            ByteArrayOutputStream bstr = new ByteArrayOutputStream();
            byte [] buffer = new byte[1024]; //每一次读取文件的长度
            int len = 0;
            try {
                while((len = fis.read(buffer)) !=-1) // 文件是否已被读取完整
                {
                    bstr.write(buffer,0,len);
                }
                content = bstr.toString();
                fis.close();
                bstr.close();


写入文件

   FileOutputStream fos =  openFileOutput("first.txt",MODE_PRIVATE + MODE_NO_LOCALIZED_COLLATORS); //后面一个参数设置文件的权限,不同的权限使用加号连接
            try {
                fos.write(content.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }

0 0
原创粉丝点击