Android中FileInputStream、FileOutputStream及flush()方法的使用

在Android中进行文件读写操作可以使用FileInputStream和FileOutputStream。

Android中文件读写的原理:

(1)所有文件的存储都是以字节的方式存储。

(2)磁盘中存储的并不是文件的字符,而是将字符编码成字节,再存储到磁盘。

(3)在读取文件时,是一个字节一个字节的读取以形成字节序列。

 

文件读写的步骤:

(1)建立通道。

(2)定义存储空间。

(3)读写数据。

(4)先执行flush()方法,然后关闭流。

 

输入流与输出流:

(1)程序从输入流读取数据。

(2)程序向输出流写数据。

 

实例代码:

                    //建立通道对象
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    Log.d(TAG, "readUDiskDevsList: "+temp.getName()+"复制成功");
                    //定义存储空间
                    byte[] b = new byte[1024 * 5];
                    //开始读文件
                    int len;
                    while ((len = input.read(b)) != -1) {
                        //将b中的数据写入到FileOutputStream对象中
                        output.write(b, 0, len);
                    }
                    //关闭流
                    output.flush();
                    output.close();
                    input.close();

 

实现将文件夹中的文件复制到另一个路径,oldPath->newPath

private void readUDiskDevsList(String oldPath, String newPath) {
        try {
            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            //读取文件夹中的文件,以字节流的方式
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    //建立通道对象
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    Log.d(TAG, "readUDiskDevsList: "+temp.getName()+"复制成功");
                    //定义存储空间
                    byte[] b = new byte[1024 * 5];
                    //开始读文件
                    int len;
                    while ((len = input.read(b)) != -1) {
                        //将b中的数据写入到FileOutputStream对象中
                        output.write(b, 0, len);
                    }
                    //关闭流
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {//如果是子文件夹
                    readUDiskDevsList(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
            Toast.makeText(this, "copy完成,请拔出U盘", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();
        }


    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值