FileInputStream及FileOutputStream

本文详细介绍了Java中的FileInputStream用于字节输入流文件操作,包括单字节和批量读取,并展示了FileOutputStream如何将数据写入文件,讨论了两种创建FileOutputStream的方法区别。
摘要由CSDN通过智能技术生成

FileInputStream

类的继承关系

在这里插入图片描述

方法摘要

在这里插入图片描述

演示FileIputStream的使用(字节输入流 文件–>程序)

package com.cyr.IOStream.inputStream;

import org.junit.Test;

import java.io.IOException;
import java.io.FileInputStream;

/**
 * @author chen
 * @version 1.0
 * 演示FileInputStream的使用(字节输入流 文件--》程序)
 */

public class FileInputStream_ {

    /**
     * 演示读取文件
     * 单个字节读取,效率比较低
     * 所以使用read(byte[] b)
     */
    @Test
    public void readFile01(){
        String filePath = "e:\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //这里使用的read是[read() int]
            //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
            //如果返回-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();
            }
        }
    }
    /**
     * 使用read(byte[] b)读取文件,提高效率
     */
    @Test
    public void readFile02(){
        String filePath = "e:\\hello.txt";
        int readData = 0;
        //字节数组
        byte[] buf = new byte[8]; //一次读取8个字节
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //这里使用的read是[read() int]
            //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
            //如果返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while ((readLen = fileInputStream.read(buf)) != -1){
                System.out.print(new String(buf,0,readLen));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


FileOutputStream

类的继承关系

在这里插入图片描述

方法摘要

在这里插入图片描述

演示FileOutputStream将数据写到文件中

package com.cyr.IOStream.outputStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author chen
 * @version 1.0
 */
public class FileOutputStream01 {

    /**
     * 演示使用FileOutputStream 将数据写到文件中
     * 如果该文件不存在,则创建该文件
     */
    @Test
    public void writeFile(){
        //创建FileOutputStream对象
        String filePath = "e:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {
            //得到fileOutputStream对象
            //注意:
            //1.new FileOutputStream(filePath);创建方式,当写入内容时,会覆盖原来的内容
            //2.new FileOutputStream(filePath,true);创建方式,当写入内容时,是追加到文件后面
            fileOutputStream = new FileOutputStream(filePath);
            //写入一个字节
            //fileOutputStream.write('H');

            //写入一个字符串
            String str = "hello,world";
            //str.getBytes()可以把字符串转成byte[]字节数组
            fileOutputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值