Java Stream 流对象(实用技巧)

目录

一、InputStream & OutputStream

1.1、InputStream 和 OutputStream 一般使用

1.2、特殊使用

1.2.1、如何表示文件读取完毕?(DataInputStream)

1.2.2、字符读取/文本数据读取(Scanner)

1.2.3、文件的随机读写(RandomAccessFile)


一、InputStream & OutputStream


1.1、InputStream 和 OutputStream 一般使用

InputStream 有以下几个方法:

  1. int read():读取一个字节的数据,返回 -1 代表已经完全读完了.
  2. int read(byte[] b):最多读取 b.length 字节的数据到 b 中,返回实际读到的数 量;-1 代表以及读完了(这就像是你去端了个盆,去食堂让阿姨给打饭,那么阿姨肯定是按照她这饭的多少,能给你打满,就尽量给你打满),这也是实际比较常用的方法.
  3. int read(byte[] b, int off, int len):最多读取 len - off 字节的数据到 b 中,放在从 off 开始,返回实际读到的数量;-1 代表以及读完了.
  4. void close():关闭字节流(一般会把 InputStream 写在 try() 中,就不用手动释放了~).

inputStream 只是一个抽象类,要使用还是需要具体的实现类,比如 当客户端和服务器 accept 后,获取流对象具体实现类...... 但是我们最常用的还是文件的读取,也就是 FileInputStream.

OutputStream 有以下几个方法:

  1. void write(int b):将指定的字节写入此输出流.
  2. void write(byte[] b):将 b 这个字符数组中的数据全部写入 os 中.
  3. int write(byte[] b, int off, int len):将 b 这个字符数组中从 off 开始的数据写入 os 中,一共写 len 个
  4. void close():关闭字节流
  5. void flush():我们知道 I/O 的速度是很慢的,所以,大多的 OutputStream 为了减少设备操作的次数,在写数据的时候都会将数据先暂时写入内存的一个指定区域里,直到该区域满了或者其他指定条件时才真正将数据写入设备中,这个区域一般称为缓冲区。但造成一个结果,就是我们写的数据,很可能会遗留一部分在缓冲区中。需要在最后或者合适的位置, 调用 flush(刷新)操作,将数据刷到设备中。

OutputStream 同样只是一个抽象类,要使用还需要具体的实现类。我们现在还是只关心写入文件中, 所以使用 FileOutputStream

Ps:FileOutputStream 有一个构造器是 new FileOutputStream(String path, boolean append),第一个参数是文件路径,第二个参数是是否以追加到末尾的形式写入,这里如果要在文件末尾追加数据,就需要填写 true 即可~

1.2、特殊使用

1.2.1、如何表示文件读取完毕?(DataInputStream)

使用 read() 方法,返回一个 int 值,这个值如果是 -1,表示文件已经全部读取完毕~

但是实际的项目中,还常常使用一种顺水推舟方式表示文件读取完毕~ 如果我们约定数据的格式,是一个 int (表示 payload 的长度 )+ payload,后面也是一样格式的数据,那么这个时候,我们就需要通过 DataInputStream (这个流对象专门用来读取数字和字节流,必须搭配 DataOutputStream 使用)中的 readInt 方法来读取 这个 int,这个方法特殊就在于读取到文件末尾以后,继续读取就会抛出 EOFException 这个异常(以往我们读取到文件末尾都是返回 -1,或者是 null。),因此这里我们就可以 通过 catch 来捕获这个异常,表示读取完成~

Ps:值得注意的是,DataInputStream / DataOutputStream 可以方便进行数字的读写(readInt、writeInt),原生的 InputStream / OutputStream 没有提供数字读写方法,需要我们自己转化.

    public LinkedList<Message> loadAllMessageFromQueue(MSGQueue queue) throws IOException {
        //1.检查文件是否存在
        if(!checkQueueFileExists(queue.getName())) {
            throw new IOException("[MessageFileManager] 获取文件中所有有效消息时,发现队列文件不存在!queueName=" + queue.getName());
        }
        //2.获取队列中所有有效的消息
        synchronized (queue) {
            LinkedList<Message> messages = new LinkedList<>();
            try (InputStream inputStream = new FileInputStream(getQueueDataFilePath(queue.getName()))) {
                try (DataInputStream dataInputStream = new DataInputStream(inputStream)) {
                    int index = 0;
                    while(true) {
                        int messageSize = dataInputStream.readInt();
                        byte[] payload = new byte[messageSize];
                        int n = dataInputStream.read(payload);
                        if(n != messageSize) {
                            throw new IOException("[MessageFileManager] 读取消息格式出错!expectedSize=" + messageSize +
                                    ", actualSize=" + n);
                        }
                        //记录 offset
                        Message message = (Message) BinaryTool.fromBytes(payload);
                        if(message.getIsValid() == 0x0) {
                            index += (4 + messageSize);
                            continue;
                        }
                        message.setOffsetBeg(index + 4);
                        message.setOffsetEnd(index + 4 + messageSize);
                        messages.add(message);
                        index += (4 + messageSize);
                    }
                }
            } catch (EOFException e) {
                System.out.println("[MessageFileManager] 队列文件中有消息获取完成!queueName=" + queue.getName());
            }
            return messages;
        }
    }

1.2.2、字符读取/文本数据读取(Scanner)

对字符类型直接使用 InputStream 进行读取是非常麻烦且困难的,所以,我们使用一种我们之前比较熟悉的类来完成该工作,就是 Scanner 类。

Scanner 一般搭配 PrintWrite ,进行文本格式数据的读写,大大省去了 InputStream/OutputStream  还需要将 字节数据 和 文本数据 之间使用 UTF-8 解码转换的操作.

例如一:

// 需要先在项目目录下准备好一个 hello.txt 的文件,里面填充 "你好中国" 的内容

public class Main {
    public static void main(String[] args) throws IOException {
        try (InputStream is = new FileInputStream("hello.txt")) {
           try (Scanner scanner = new Scanner(is, "UTF-8")) {
               while (scanner.hasNext()) {
                   String s = scanner.next();
                   System.out.print(s);
               }
           }
       }
   }
}

例如二: 

    public void writeStat(String queueName, Stat stat) {
        try (OutputStream outputStream = new FileOutputStream(getQueueStatFilePath(queueName))) {
            PrintWriter printWriter = new PrintWriter(outputStream);
            printWriter.write(stat.totalCount + "\t" + stat.validCount);
            printWriter.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Stat readStat(String queueName) {
        Stat stat = new Stat();
        try (InputStream inputStream = new FileInputStream(getQueueStatFilePath(queueName))) {
            Scanner scanner = new Scanner(inputStream);
            stat.totalCount = scanner.nextInt();
            stat.validCount = scanner.nextInt();
            return stat;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

1.2.3、文件的随机读写(RandomAccessFile)

之前使用 DataInputStream / DataOutputStream 都是接收 FileInputStream/FileOutputStream 进行文件的顺序读写(要么是从头读到尾,要么是在尾部追加写入......),RandomAccessFile 类就特别在可以任意指定位置进行 读/写 操作!

这里涉及到光标的概念,实际上就是你写文件的时候,你写到哪个位置,哪个位置就会有一个光标一闪一闪~

在 RandomAccessFile 中,可以使用 seek() 方法指定光标的位置(单位是字节),例如你要对一个文件中的某一段内存进行逻辑删除(没有实际删除,只是先读出来标记为无效,然后在写回文件,回收站就差不多是这个逻辑).

    public void deleteMessage(MSGQueue queue, Message message) throws IOException {
        //1.检查队列相关文件是否存在
        if(!checkQueueFileExists(queue.getName())) {
            throw new IOException("[FileDataCenter] 删除消息时,发现队列相关文件不存在!queueName=" + queue.getName());
        }
        synchronized (message) {
            //2.将要删除的消息文件读出来
            try (RandomAccessFile randomAccessFile = new RandomAccessFile(getQueueDataFilePath(queue.getName()), "rw")) {
                randomAccessFile.seek(message.getOffsetBeg() - 4);
                int payloadSize = randomAccessFile.readInt();
                byte[] payload = new byte[payloadSize];
                int n = randomAccessFile.read(payload);
                if(n != payloadSize) {
                    throw new IOException("[FileDataCenter] 读取文件格式出错!path=" + getQueueDataFilePath(queue.getName()));
                }
                //3.将待删除的消息标记为无效(isValid = 0x0)
                Message toDeleteMessage = (Message) BinaryTool.fromBytes(payload);
                toDeleteMessage.setIsValid((byte) 0x0);
                //4.将消息写入文件
                randomAccessFile.seek(message.getOffsetBeg());
                randomAccessFile.write(BinaryTool.toBytes(toDeleteMessage));
            }
            //5.更新统计文件
            Stat stat = readStat(queue.getName());
            stat.validCount -= 1;
            writeStat(queue.getName(), stat);
        }
    }

Ps:

RandomAccessFile 的有两种构造器(实际上是一种),RandomAccessFile(String name, String mode)等价于RandomAccessFile(new File(name), String mode)

mode 这个参数表示 访问模式~
➢ "r":以只读方式打开指定文件。如果试图对该RandomAccessFile执行写入方法,都将抛出IOException异常。
➢ "rw":以读、写方式打开指定文件。如果该文件尚不存在,则尝试创建该文件。
➢ "rws":以读、写方式打开指定文件。相对于"rw"模式,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
➢ "rwd":以读、写方式打开指定文件。相对于"rw"模式,还要求对文件内容的每个更新都同步写入到底层存储设备。

猜你喜欢

转载自blog.csdn.net/CYK_byte/article/details/132724326
今日推荐