IO_文件字节流

FileOutputStream

向文件写入内容,若文件不存在则自动创建文件(前提是目录存在)

@Test
    //向文件输出数据 若文件不存在则一同创建文件(前提是目录存在)
    public void fileWrite01(){
        String filePath = "E:\\fileStream.txt";
        String buf = "hello";
        FileOutputStream fileOutputStream = null;
        try {
            //获得fileOutputStream对象
            //默认写入时进行覆盖写入 若new FileOutputStream(filePath,true)则在原有文件内容末尾追加内容 不覆盖写入
            fileOutputStream = new FileOutputStream(filePath);
            try {
                //写入一个字节
                //fileOutputStream.write('h');

                //写入字符串:调用getBytes()方法将字符串转换为字节数组
                fileOutputStream.write(buf.getBytes());

                //也可以指定写入的字符串长度 0代表起始写入位置 buf.length()代表写入长度(均自定)
                //fileOutputStream.write(buf.getBytes(),0,buf.length());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

FileInputStream

从文件中读取内容(前提是文件存在)

01.使用read()方法从流中读取文件内容。(逐个字节读取,效率较低)

@Test
    //使用read()方法读取文件
    public void readFile01() {
        String filePath = "E:\\fileStream.txt";
        int readData = 0;
        //fileInputStream若定义在try/catch内 则无法在finally中调用 也就无法关闭流
        FileInputStream fileInputStream = null;
        try {
            //创建fileInputStream对象 用于读取readFile.txt文件
            fileInputStream = new FileInputStream(filePath);
            //read()返回一个从该输入流读取的字节 如果返回-1则表示数据全部读取完毕
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char)readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

02.使用read(byte[] b)方法(从流中)读取文件内容。(字节数组读取,效率较高。一次性读取的最大字节数取决于字节数组的最大容纳量)

@Test
    //使用read(byte[] b)方法读取文件
    //该方法效率高于read()方法
    public void readFile02() {
        String filePath = "E:\\fileStream.txt";
        byte [] buf = new byte[8];
        int readLength = 0;
        //fileInputStream若定义在try/catch内 则无法在finally中调用 也就无法关闭流
        FileInputStream fileInputStream = null;
        try {
            //创建fileInputStream对象 用于读取readFile.txt文件
            fileInputStream = new FileInputStream(filePath);
            //read(byte[] b)返回从该输入流实际读取的字节数 如果返回-1则表示数据全部读取完毕
            while ((readLength = fileInputStream.read(buf)) != -1) {
                System.out.print(new String(buf,0,readLength));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值