InputStream、OutputStream 字节输入输出流

本文介绍了Java中InputStream和OutputStream的基本使用方法,包括读取和写入字节流的具体步骤及异常处理。详细解析了输入流和输出流的主要方法及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

InputStream输入流基类

源码剖析:

abstract class InputStream implements Closeable 
//抽象类
//可以自动关闭
  • InputStream基本方法介绍:
int read()从数据中读取一个字节,返回该字节
int read(byte b[])批量读取数据,将数据读入到arr的byte数组中,返回值为int类型,表示读取的有效数据的个数
int read(byte b[], int off, int len)批量读取数据,b[] 表示的是数据读入的缓存数组,off表示读入的缓存起始位置,len表示从起始位置开始写入的数量,给read方法返回值表示读取有效数据个数
int available()返回在不阻塞的情况下可获取的字节数
synchronized void mark(int readlimit)在输入流的当前位置打一个标记,如果从输入流中已经读取的字节多于readlimit个,则这个流允许忽略这个标记。(标记作用,操作内核,重复读,需要操作系统支持)
void close()关闭流
synchronized void reset()返回到最后一个标记,随后对read的调用将重新读入这些字节。
boolean markSupported()如果这个流支持打标记,则返回true。(查看操作系统是否支持这个流打标记)
long skip(long n)在输入流中跳过n个字节,返回实际跳过的字节数。
  • 字节输入流读操作步骤:

1.打开特定类型的输入流,可能会抛出FileNotFoundException;

FileInputStream inputStream = new FileInputStream(path);

2.读取数据操作,-1表示读取结束,可能会抛出IOException异常;read()(阻塞性的方法,从内核态到用户)

int read = inputStream.read();

3.关闭流,可能会抛出IOException异常

inputStream.close();

public class InputOutStreamDemo {
    public static void Input(String path){
        try {
            //打开字节读操作流,可能会抛出异常FileNotFoundException
            FileInputStream fileInputStream = new FileInputStream(path);

            //读操作,可能会抛出异常IOException
            int read = fileInputStream.read();
            System.out.println(read);//97
            //关闭流
            fileInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String path = "C:\\Users\\a\\Desktop\\L\\inputtest.txt";//存放abc
        Input(path);
    }
}
OutputStream输出流基类

源码剖析:

abstract class OutputStream implements Closeable, Flushable
//抽象类
//可以自动关闭,可刷新缓存
  • OutputStream基本方法介绍:
void write(int b)每次写入一个字节
void write(byte b[])批量写入,写入类型为byte[]
void write(byte b[], int off, int len)批量写入,可以自定义从byte数组的起始和读取长度
void flush()刷新缓存
void close()关闭流
  • 字节输出流写操作步骤:

1.打开字节输出流,可能会抛出异常FileNotFoundException;

FileOutputStream fileOutputStream = new FileOutputStream(path);

2.写操作,可能会抛出异常IOException;

fileOutputStream.write(99);

注意: 写操作时,若文件不存在时,会自动创建文件,并写入数据;

当文件目录路径不合法时会抛出异常FileNotFoundException;

3.关闭流,可能会抛出IOException。

fileOutputStream.close();

public class InputOutStreamDemo {
public static void OutPut(String path){
        try {
            //打开字节写操作流,可能会抛出异常FileNotFoundException
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            //写操作,可能会抛出异常IOException
            fileOutputStream.write(99);
            //批量写入操作
            byte[] bytes = {'a','b'};
            fileOutputStream.write(bytes);//cab
            //fileOutputStream.write(bytes,0,1);//caba
            //fileOutputStream.write("Hello".getBytes());//cabaHello
            fileOutputStream.write("hellolucy".getBytes());//cabhellolucy
            //关闭流
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String path = "C:\\Users\\a\\Desktop\\L\\inputtest.txt";//空文件
        OutPut(path);
    }
}

注意:在写入和读取操作时,会抛出相应的异常,要进行捕获。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值