Java-I/O学习(5)

Java中的五种I/O模型详解 输入/输出(I/O)是计算机程序与外部环境(如文件、网络等)进行交互的方式。Java语言提供了多种I/O模型,帮助开发者处理数据读写和通信。本文将深入探讨Java中的五种I/O模型:阻塞I/O、非阻塞I/O、选择器I/O、NIO(New I/O)以及异步I/O,分析它们的工作原理、优缺点和适用场景,以帮助读者全面理解Java的I/O处理机制。 阅读详情

Java-I/O学习(5)

ByteArrayInputStream

ByteArrayInputStream类可以让你从一个字节数组来读取流

创建


//Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. The initial value of pos is 0 and the initial value of count is the length of buf.
ByteArrayInputStream​(byte[] buf)


//Creates ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.
ByteArrayInputStream​(byte[] buf,int offset,int length)

使用:


String s = "abcdefghijlmnopq";
        
        
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

read


//Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
//This read method cannot block.

//Returns:
//the next byte of data, or -1 if the end of the stream has been reached.
int read()


//Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner performed by System.arraycopy. The value k is added into pos and k is returned.
//This read method cannot block.

//Returns:
//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
int read​(byte[] b,int off,int len)

使用:


 public static void main(String[] args) throws IOException {

        String s = "abcdefghijlmnopq";

        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

        int data = in.read();

        while (data != -1) {

            System.out.println((char) data);
            data = in.read();
        }

        in.close();

    }

其它


//Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned.
long skip​(long n)

 public static void main(String[] args) throws IOException {

        String s = "abcdefghijlmnopq";

        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

        in.skip(5);

        int data = in.read();

        while (data != -1) {

            System.out.println((char) data);
            data = in.read();
        }

        in.close();

    }

ByteArrayOutputStream

Java IO API的ByteArrayOutputStream类允许您捕获写入到一个数组中的流的数据。你把数据写到ByteArrayOutputStream,写完之后,调用toByteArray()方法就可以以字节数组的形式获得所有的已写的数据。

创建


//Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
ByteArrayOutputStream()


//Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
ByteArrayOutputStream​(int size)


write


//Writes the specified byte to this byte array output stream.
//Parameters:
//b - the byte to be written
void write​(int b)

//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
void write​(byte[] b,int off,int len)


//Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
void writeTo​(OutputStream out)

其余方法



//Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
byte[] toByteArray()

//Returns the current size of the buffer.
int size()

使用:


public static void main(String[] args) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        String s = "1234567890";
        out.write(60);
        out.write(s.getBytes());
        for (byte b : out.toByteArray()){
            System.out.println((char)b);
        }

    }

BufferedInputStream

BufferedInputStream为你的输入流提供了一个缓冲区。缓冲区可以大大的提高IO速度。不是每次从网络或磁盘上读取一个字节,而是每次读取一大块儿内容到内部的缓冲区中。当你从BufferedInputStream读取一个字节时,你其实是从它内部的缓冲区读取的。当缓冲区已经读完,BufferedInputStream会读另一大块的数据到缓冲区中。这通常要比每次读取单字节要快的多,尤其是访问磁盘和大数据量的情况。

创建


//Creates a BufferedInputStream and saves its argument, the input stream in, for later use. An internal buffer array is created and stored in buf.
BufferedInputStream​(InputStream in)

//Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use. An internal buffer array of length size is created and stored in buf.
BufferedInputStream​(InputStream in,int size)
  public static void main(String[] args) throws FileNotFoundException {

        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\c.txt";

        FileInputStream inputStream = new FileInputStream("");

        BufferedInputStream in = new BufferedInputStream(inputStream);
        
    }

read

//See the general contract of the read method of InputStream.
//Returns:
//the next byte of data, or -1 if the end of the stream is reached.
int read()

//Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
int read​(byte[] b,int off,int len)

   String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\c.txt";

        FileInputStream inputStream = new FileInputStream(path);

        BufferedInputStream in = new BufferedInputStream(inputStream);

        int data = in.read();

        while (data != -1) {

            System.out.println(data);

            data = in.read();
        }
        in.close();

BufferedOutputStream

BufferedOutputStream 为你的输出流提供了一个缓冲区。缓冲区可以大大的提高IO速度。不是每次从网络或磁盘上读取一个字节,而是每次读取一大块儿内容到内部的缓冲区中。这通常要比每次读取单字节要快的多,尤其是访问磁盘和大数据量的情况。

创建


//Creates a new buffered output stream to write data to the specified underlying output stream.
BufferedOutputStream​(OutputStream out)

//Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
BufferedOutputStream​(OutputStream out,int size)

write


//Writes the specified byte to this buffered output stream.
void write​(int b)


//Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
void write​(byte[] b,int off,int len)


  public static void main(String[] args) throws IOException {


        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        FileOutputStream outputStream = new FileOutputStream(path,true);

        BufferedOutputStream out = new BufferedOutputStream(outputStream);

        out.write("where are you from".getBytes());

        out.close();

    }


PushbackInputStream

在JAVA IO中所有的数据都是采用顺序的读取方式,即对于一个输入流来讲都是采用从头到尾的顺序读取的,如果在输入流中某个不需要的内容被读取进来,则只能通过程序将这些不需要的内容处理掉,为了解决这样的处理问题,在JAVA中提供了一种回退输入流(PushbackInputStream、PushbackReader),可以把读取进来的某些数据重新回退到输入流的缓冲区之中。

创建


//Creates a PushbackInputStream with a 1-byte pushback buffer, and saves its argument, the input stream in, for later use. Initially, the pushback buffer is empty.
PushbackInputStream​(InputStream in)


//Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use. Initially, the pushback buffer is empty.
PushbackInputStream​(InputStream in,int size)

unread


//Pushes back a byte by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value (byte)b.
void unread​(int b)

//Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[off], the byte after that will have the value b[off+1], and so forth.
void unread​(byte[] b,int off,int len)

//Pushes back an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[0], the byte after that will have the value b[1], and so forth.
void unread​(byte[] b)

   public static void main(String[] args) throws IOException {

        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        FileInputStream inputStream = new FileInputStream(path);

        PushbackInputStream in = new PushbackInputStream(inputStream);

        int data = 0;

        while ((data = in.read()) != -1) {

            if ((char)data == '5'){

                in.unread(data);
                data = in.read();
//                System.out.println("unread " +(char)data);
            } else {
                System.out.println((char)data);
            }

        }
    }

SequenceInputStream

SequenceInputStream可以将两个或更多个InputStream合并成一个。它会首先读取第一个的全部字节,然后第二个。这就是叫SequenceInputStream的原因,因为它是安顺序读取的。

创建


//Initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.
SequenceInputStream​(InputStream s1,InputStream s2)

//Initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream. The input streams that are produced by the enumeration will be read, in order, to provide the bytes to be read from this SequenceInputStream. After each input stream from the enumeration is exhausted, it is closed by calling its close method.
SequenceInputStream​(Enumeration<? extends InputStream> e)


   public static void main(String[] args) throws IOException {


        String path1 = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        String path2 = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t2.txt";

        FileInputStream f1 = new FileInputStream(path1);

        FileInputStream f2 = new FileInputStream(path2);

        SequenceInputStream in = new SequenceInputStream(f1,f2);

        int data = 0;

        while ((data = in.read()) != -1) {

            System.out.println((char) data);

        }

        in.close();

    }

【Java】I/O 流篇 —— 字节 I/O 流 I/O流(Input/Output Stream)是计算机中数据传输的一种抽象概念,用于描述数据在程序和外部设备(如文件、网络、内存等)之间的流动方式。可以将它想象成一条“数据管道”,数据像水流一样在这条管道中连续传输。作用:I/O 流用于读写文件中的数据(可以是读写文件,或网络中的数据)。读(Read) → Input(输入)当程序从外部(如文件、键盘、网络等)获取数据时,称为“读”操作。例如:读取文件内容、接收用户键盘输入、获取网络数据等。这属于输入(Input) 阅读详情

相关推荐

MRI多序列,多中心数据集预处理--使用FSL-Flirt 配准数据

FLIRT(FMRIB 的线性图像配准工具)是用于线性(仿射)模态内和模态间脑图像配准的全自动稳健且准确的工具。 什么是配准 在这两张图片上,很容易看出它们没有对齐,左边相对右边倾斜,虽然我们人类视觉系统还是能把对应的解剖关系连系在一起,但是我们使用计算机分析时确不能够 因此,我们希望相同的解剖结构对齐,用坐标表示的话,希望他们在相同的坐标上。如下图 对齐这些解剖结构的过程就叫做配准(register) 因此,配准的目的也是使相同的解剖结构在同一个位置,便于分析。可以将CT和MRI配准,看同一结构在两个

Tina姐的博客 1万+

java中IO(三):内存流简单讲解

java中IO(三):内存流简单讲解 介绍 在io(二)当中介绍的都是关于文件进行简单的IO处理,除了文件了IO处理之外,其实还有一种内存的IO操作,对于内存的操作流我们称为内存操作类。内存操作流主要有:ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter这四个类 讲解 一、ByteArrayInp...

bugcat的博客 1169

rockchip rk3566 android11 网口log报错: DMA engine initialization failed

问题描述:上电初始化前将网口插入,然后上电初始化网口能够正常使用,且能够找到PHY,ifconfig 查看能够有eth0 产生,网口正常使用且能够热插拔,但上电初始化时,不插入网口,就会报DMA engine initialization failed 错误,DMA 初始化的时候出错了。 分析:一般产生这个问题可以认为是GMAC 的工作时钟出问题了。先测量时钟引脚是否有时钟,时钟频率以及幅度等指标是否正常,主要确认以下几个方面: 1.IOMUX 出错,检查时钟脚寄存器值是否正确。 2.时钟方向以及配置

qq_48709036的博客 2万+

Java I/O 教程(六) BufferedInputStream 类

Java BufferedInputStream Class Java BufferedInputStream class 用于从输入流读取数据,和BufferedOutStream一样内部使用缓冲机制提高读取性能。 需要注意的是: 当读取流中字节时,内部缓冲区每次会自动重新填满多个字节。 当一个缓冲输入流创建时,一个内部缓冲区数组也会创建。 类定义

老徐的博客只有干货 2345

snipersim安装

snipersim安装记录,便于后续使用tip:snipersim需要在python2的环境下编译。

weixin_53507617的博客 917

PaddleOCR多进程GPU推理实战:200万图片高效处理方案

本文详细介绍了PaddleOCR多进程GPU推理在高效处理200万张图片OCR任务中的实战方案。通过优化GPU利用率、多进程并发和关键参数调优,实现了从单进程3000张/小时到8进程3万张/小时的性能飞跃,为大规模OCR处理提供了可靠解决方案。

prometheus9mon的博客 260

【Java】关于ByteArrayOutputStream的源码分析

一、问题 1.1、环境 电脑环境:Windows 10; 开发工具:IntelliJ IDEA; 数据库环境:Redis 3.2.100 JDK环境: Jdk1.8; 1.2、问题 ByteArrayOutputStream 是经常用到的字符数组输出流,下面我们就来分析一下该类;JDK 1.8的 ByteArrayOutputStream 源码请见附录; 二、解答 1、概述 ByteArrayOutputStream 实现了OutputStream类,这个缓冲数据,会自动根据写入的数据而增长;数组里的数据还

陶洲川的博客 974

国产操作系统替换方案:3个避坑指南

坑核心误区避坑核心原则关键动作坑一:选型只看信创信创目录=唯一标准兼容性优先,信创是约束三维评估(兼容性+信创+生态),分场景选型坑二:迁移只管能跑功能能跑=迁移成功性能确认+运维回归适配-压测-回归三阶段,基线对比坑三:不防二次迁移解决停服=完成任务一步到位,两级兼容信创路线优先,双轨制发布,终态规划。

weixin_43524706的博客 294

ByteArrayInputStream 介绍

这个类在内存中创建了一个基于字节数组的缓冲区,使得你可以像操作常规输入流一样从该缓冲区读取数据。是 Java 中的一个类,它是 java.io 包的一部分,用于实现一个从。对象,以便能够利用标准的 InputStream API 进行读取。

张紫娃的博客 573

如何用AP525 测试输入信号的相位,频响,延时,Pop和卡顿

假设信号不是从AP输出,是功放或者其他的播放设备输出的信号,接入到AP525的输入端。

帮助100万人颠覆负面潜意识,实现人生蜕变,培养1万名潜意识开发导师 2397

ADS基础教程24 - Gerber文件的导入

一、引言二、基本概念1.仿真文件下载2.仿真文件介绍二、导入步骤1.新建workspace2.选择Layout结构3.导入设计4.选择文件类型5.导入文件6.预览文件内容7.铜皮离散问题四、总结选择。通过本文章结束,可以了解ADS中如何将导入Gerber文件,并且对导入的铜皮进行合并。下一步就针对导入的文件进行层叠设置和编辑。END!}}END!

XZ_ZC的博客 3048

J2Me学习输入流--ByteAraayInputStream

java.io Class ByteArrayInputStream java.lang.Object extended byjava.io.InputStream extended byjava.io.ByteArrayInputStream public class ByteArrayInputStream extends InputStream A ...

sydx2009的博客 445

YOLOv11数据集增强 | 引入多种数据集增强方法,支持图像和标签同步扩充增强

本文介绍了YOLOv11目标检测中的数据集增强技术,重点解决图像与标注标签同步扩充的关键问题。文章系统分析了三类增强方法:几何变换(翻转、旋转、缩放)、颜色变换(亮度、对比度调整)和高级增强(Mosaic、MixUp),并探讨了其在小样本检测、复杂场景泛化和多方向目标识别等场景的应用价值。针对核心的标签同步问题,提供了基于Python和OpenCV的详细代码实现示例,展示如何对图像进行水平翻转和缩放操作时精确调整YOLO格式边界框坐标。该技术能有效解决训练数据不足和多样性匮乏问题,显著提升模型的泛化性能。

走向CTO的路上... 1511

java基础——java I/O学习笔记5

看视频笔记~ I/O解决的问题:解决设备与设备之间的数据传输问题。如:硬盘------>内存    内存-------->硬盘 字节流: 输入字节流: ---------| InputStream 所有输入字节流的基类。 抽象类。 ------------| FileInputStream 读取文件的输入字节流。 ------------| BufferedInputStream 缓冲

Magaret的博客 156

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

java中的I/O演变基础读书笔记

本文参考自《Netty权威指南(第2版)》,李林峰著。在学习的同时也顺便记录下来自己的心得,让自己更加深刻的理解这些知识 1.Linux网络I/O模型简介 1.1 I/O的概念 I/O系统,英文全称为“Input output system”,中文全称为“输入输出系统”,由输入输出控制系统和外围设备两部分组成,是计算机系统的重要组成部分。 在计算机系统中,通常把处理器和主存储器之外的部分称为输入输...

BigHusky的博客 223

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Java I/O模型的演变:从阻塞I/O到异步I/O的底层机制解析

以上代码示例展示了各种I/O模型的基本用法,从阻塞I/O的简单粗暴,到异步I/O的高效并发,每种模型都有其适用的场景。理解这些模型的底层机制,可以帮助我们根据实际需求选择合适的I/O模型,编写出高性能、高并发的Java应用程序。I/O模型的演进是为了解决并发问题,选择哪种模型取决于具体场景。阻塞I/O简单但并发低,非阻塞I/O解决了阻塞但消耗CPU,I/O多路复用提高并发,异步I/O实现了真正的高效并发。

weixin_41455464的博客 1495

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

什么是I/O?

计算机有输入设备(键盘等)和输出设备(显示屏等),(网卡和硬盘既可以作为输入也可以作为输出)。所以从计算机结构角度来看,I/O描述的是。为了保证操作系统的安全性和稳定性,一个进程的地址空间被分为和。我们平时的应用程序都是运行在用户空间,而像等都是需要依赖内核空间来完成的。而我们要执行I/O操作时,由于我们没有权限,所以只有,让操作系统来帮我们完成。而我们一般开发常见的就是。所以从应用程序来看,I/O是指我们的应用程序,将我们需要的数据从内核空间拷贝到用户空间。

m0_51808048的博客 6725

Java的I/O演进之路

一、I/O 基础入门 Java1.4之前的早期版本,对I/O的支持并不完善,开发人员在开发java高性能IO程序的时候,会面临一些巨大的挑战和困难,主要问题如下: 没有数据缓存区,IO性能存在问题 没有C或者C++的Channel概念,只有输入、输出流 同步阻塞式I/O通信(BIO),通常会让通讯线程长时间的阻塞服务端的响应 支持的字符集有限,硬件可移植性不好 在Java支持异步通讯之前的很长一段时间里,一直都是C/C++长期占据着高性能服务端程序开发的语言,Java的同步阻塞式I/O一直被人们锁所诟病

csdn5215的博客 8016

Java的I/O历史

Java的I/O历史

AiFlutter的博客 1251

Java技术——I/O知识学习

0. 前言 I/O是Java技术网络中一个比较重要的点,不仅是平时开发中的家常便饭,也是面试的时候经常被问到的话题。本篇将介绍字节IO、字符IO的基本体系和用法,最后总结一下NIO的一些知识。1. 字节I/O字节流处理单元为1个字节,主要用在处理二进制数据,字节用来与文件打交道,所有文件的储存都是通过字节(byte)的方式,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节...

关于Android开发的一些技术点总结 ╮( ̄▽ ̄”)╭ 1万+

java学习——I/O

/根据提供的文件信息构建一条文件输出通道,并根据append的值决定是将内容追加到末尾还是直接覆盖public Fileoutputstream(File file, boolean append) throws FileNotFoundException;磁盘和内存是两个不同的设备,它们之间要实现数据的交互,就必须要建立一条通道, 在Java中实现建立这样的通道的是I/0流。到目前为止,我们看到的大多数示例都使用无缓冲的I/O。正如二进制数据流支持原始数据类型的I/O一样,对象流也支持对象的I/O。

ping12138的博客 930

Java NIO编程理论基础篇——Java I/O的发展以及linux网络I/O模型

发展简述: Java在jdk1.4之前,Java对I/O的支持并不完善,开发人员会遇到许多问题,例如: 没有数据缓冲区,性能较低; 没有C或者C++中的Channel概念,通常 ...

sdr_zd的博客 5677

Java的I/O模型

不同的I/O模型由于线程模型、API等差别很大,所以用法的差异也非常大。

fengshi95的博客 908

Java—I/O操作

IO 即,输入和输出。数据输入到计算机内存的过程即输入,反之输出到外部存储(比如数据库,文件,远程主机)的过程即输出。数据传输过程类似于水流,因此称为 IO 流。IO 流在 Java 中分为输入流和输出流,而根据数据的处理方式又分为字节流和字符流。Java I0 流的 40 多个类都是从如下 4 个抽象类基类中派生出来的。InputStream/Reader: 是所有的输入流的基类,InputStream是字节输入流,Reader是字符输入流。

m0_54049074的博客 1034

Java 网络 I/O 编程:同步阻塞 I/O,同步非阻塞 I/O 和 I/O 多路复用

Java 网络 I/O 编程:同步阻塞 I/O,同步非阻塞 I/O 和 I/O 多路复用

lybugproducer的博客 1363

Java I/O

在 Java 中,I/O 模型主要分为三种:BIO(Blocking I/O,同步阻塞 I/O)、NIO(Non-blocking I/O,同步非阻塞 I/O) 和 AIO(Asynchronous I/O,异步非阻塞 I/O)。它们分别代表了不同的 I/O 处理方式,适用于不同的应用场景。Java 的 I/O(输入/输出)是 Java 编程中非常重要的一部分,用于处理数据的读取和写入。在 Java 的 I/O 中,同步/异步和阻塞/非阻塞是两个重要的概念,它们描述了 I/O 操作的行为模式和性能特性。

qq_33807380的博客 699
上一篇: Java-I/O学习(4)
下一篇: Java-I/O学习(6)
Coder_py
博客等级 码龄10年 106粉丝 258原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值