InputStream的read()读取机制

article/2025/9/21 11:22:28

public void readArr() {// 明确文件File file = new File("D:/net.txt");// 构建流的对象InputStream inputStream = null;try {inputStream = new FileInputStream(file);// 声名缓冲数组int i;byte[] bytes = new byte[5];while ((i = inputStream.read(bytes)) != -1) {for (int j = 0; j < i; j++) {System.out.print((char)bytes[j]);}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (inputStream != null) {inputStream.close();}} catch (IOException e) {e.printStackTrace();}}
}

在使用InputStream读取文件时,发现在使用while循环读取文件时,它会自动的按顺序读取文件的内容,这是为什么呢?首先我们看看官方的API文档解释:


/**
* Reads the next byte of data from the input stream. The value byte is
* returned as an <code>int</code> in the range <code>0</code> to
* <code>255</code>. If no byte is available because the end of the stream
* has been reached, the value <code>-1</code> is returned. This method
* blocks until input data is available, the end of the stream is detected,
* or an exception is thrown.
*
* <p> A subclass must provide an implementation of this method.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* stream is reached.
* @exception IOException if an I/O error occurs.
*/
public abstract int read() throws IOException

大概意思就是,每次调用InputStream.read()方法,就从输入流中读取一个字节的数据,并返回这个字节。如果读取内容到达文件末尾,那么就返回-1。
在这里插入图片描述
文件流FileInputStream的读取是单向的,也就是说读取顺序是按照文件中的数据存储书序来的。==另外,通过.read()方法读出来的数据是个临时变量,java会自动在堆中为其分配一个内存空间,但是当.read()方法执行结束,垃圾回收器会立刻将其删除,因此在程序中.read(byte[] bytes)方法中的bytes参数才是实际上是用来存储读取出来数据的参数。==如果文件保存了10个字节的数据,而bytes长度为8,那么inputStream会按照每8个字节读一次文件,在此例中会读取两次,并且最终输出结果会有问题。这是因为第二次读取出来的两个字节会按照读取顺序依次填充在bytes数组的前两位,而后面6位元素并不会改变。

public void readStr() {// 写文件OutputStream outputStream = null;// 读文件InputStream inputStream = null;try {outputStream = new FileOutputStream(file);inputStream = new FileInputStream(file);createFile();String str = "helloWorld";outputStream.write(str.getBytes());outputStream.flush();byte[] bytes = new byte[8];// read()按内容存储顺序进行读取,从第一个字节到最后,读取出来的内容保存在bytes数组中,如果数组的长度不够,则接下来读取出来的内容会被依次覆盖while (inputStream.read(bytes) != -1) {System.out.println("我在循环!");for (byte b:bytes) {System.out.print((char)b);}System.out.println();}} catch (IOException e) {e.printStackTrace();} finally {try {outputStream.close();inputStream.close();} catch (IOException e) {e.printStackTrace();}}
}

输出结果:我在循环!
helloWor
我在循环!
ldlloWor


http://chatgpt.dhexx.cn/article/g5NySDHI.shtml

相关文章

java中的InputStream,OutputStream,Read,Writer

Java 中定义了两种类型的流&#xff1a;字节型&#xff0c;和字符型。 字节流&#xff1a;处理字节的输入和输出。包括读写二进制数据等方面的内容。 字符流&#xff1a;处理字符的输入和输出。他采用的是 Unicode 编码&#xff0c;可以实现国际化。使用字符流的另外一个好处…

InputStreamReader和OutputStreamWriter 的区别和用法

一、InputStreamReader 用于将一个字节流中的字节解码成字符 &#xff0c; 用法如下 Testpublic void Test19() throws Exception {InputStream in new FileInputStream("C:/hello.txt");// 读取文件的数据,注意文件编码为UTF-8,防止读取乱码// 将输入的字节流 ---…

InputStream.read() 和 OutputStream.write()方法

InputStream.read() 和 OutputStream.write()方法组合使用可以完成文件的复制功能。 先贴出代码 InputStream inputStream new FileInputStream(file);OutputStream os response.getOutputStream();byte[] b new byte[2048];int size;while ((size inputStream.read(b)) &…

InputStream 、 InputStreamReader和BufferedReader

在Java中&#xff0c;上述三个类经常用于处理数据流&#xff0c;下面介绍一下三个类的不同之处以及各自的用法。 InputStream &#xff1a; 是所有字节输入流的超类&#xff0c;一般使用它的子类&#xff1a;FileInputStream等&#xff0c;它能输出字节流&#xff1b;InputStre…

Java转换流(InputStreamReader/OutputStreamWriter)

文章目录 概述为什么会有转换流&#xff1f;InputStreamReaderOutputStreamWriter 概述 转换流是字节流到字符流的桥梁&#xff0c;在转换的过程中&#xff0c;可以指定编码。转换流也是一种处理流&#xff0c;它提供了字节流和字符流之间的转换。 转换流的两个类 InputStrea…

InputStream read()方法详解

在Java7中&#xff0c;InputStream被定义为一个抽象类&#xff0c;相应的&#xff0c;该类下的read()方法也是一个抽象方法&#xff0c;这也就意味着必须有一个类继承InputStream并且实现这个read方法。   查阅Java7 API&#xff0c;我们可以看到&#xff0c;在InputStream中…

【Java学习笔记】InputStreamReader的理解

一、InputStreamReader类 API文档说明&#xff1a;InputStreamReader类是从字节流到字符流的桥接器&#xff1a;它使用指定的字符集读取字节并将它们解码为字符。 它使用的字符集可以通过名称指定&#xff0c;也可以明确指定&#xff0c;或者可以接受平台的默认字符集。每次调…

InputStreamReader介绍使用

InputStreamReader类&#xff1a; java.io.InputStreamReader extends Reader InputStreamReader:是字节流通向字符流的桥梁&#xff1a;他使用指定的charset读取字节并将其解码为字符。(解码&#xff1a;把看不懂的变为能看懂的) 继承自父类的共性成员方法&#xff1a;int r…

jconsole工具监测jvm

背景 本篇文章为了记录如何使用jconsole工具。 jconsole工具使用环境是windows&#xff0c;监控服务端在linux上。 如何启动jconsole 找到你本地jdk的目录&#xff0c;进入bin目录&#xff0c;找到jconsole.exe程序双击启动即可 本地进程 如果是想调试本地程序的话&#xf…

jconsole远程连接服务器失败,提示“连接失败:是否重试“

操作步骤: 运行程序的时候加上参数: -Djava.rmi.server.hostname****** -Dcom.sun.management.jmxremotetrue -Dcom.sun.management.jmxremote.port9991 -Dcom.sun.management.jmxremote.authenticatefalse -Dcom.sun.management.jmxremote.sslfalse 服务器打开端口:效果就…

java jconsole 远程连接_jconsole连接远程tomcat

咱们先说怎么做&#xff0c;然后再聊聊一些原理&#xff0c;当然&#xff0c;本人技术能力有限&#xff0c;有错误的地方&#xff0c;欢迎指正。 1.本机环境&#xff0c;远程环境 本机环境:windows 7 java version "1.8.0_121" 远程环境:centos 6.5 java…

jconsole远程连接的使用

背景 在项目做性能压测的时候&#xff0c;对程序进行分析&#xff0c;需要用到jconsole工具&#xff0c;以前都没用过&#xff0c;学习了一下 连接步骤 1. 打开工具 打开jdk安装目录 -> bin &#xff0c;找到jconsole.exe 双击打开 2. 到服务器中&#xff0c;加入以下…

简单使用Jconsole

引言 本文简单介绍Jconsole&#xff0c;死锁用例测试分析线程 正文 测试用例 DeadLockDemo &#xff1a; public class DeadLockDemo {private static Object resource1 new Object();//资源 1private static Object resource2 new Object();//资源 2public static void…

linux jconsole 监控jvm,JVM监测分析JConsole

一、基本操作 启动界面 1.JConsole是什么 从Java 5开始引入了JConsole。JConsole是一个内置Java性能分析器&#xff0c;可以从命令行或在GUI shell中运行。您可以轻松地使用JConsole(或者&#xff0c;它更高端的“近亲” VisualVM)来监控Java应用程序性能和跟踪Java中的代码。 …

Java应用程序监控之 jconsole

jconsole jconsole 启动一个图形控制台&#xff0c;使您可以监视和管理Java应用程序。JConsole图形用户界面是一个符合Java Management Extensions&#xff08;JMX&#xff09;规范的监视工具。JConsole使用Java虚拟机&#xff08;Java VM&#xff09;的广泛工具来提供有关Jav…

JConsole远程监控配置

首先&#xff0c;看本机&#xff08;Windows&#xff09;安装了JRE没 Win 》 CMD 打开命令窗口 如有安装&#xff0c;则会显示以下版本信息&#xff1b;若没有显示&#xff0c;就安装吧 1 C:\Users\Administrator>java -version 2 java version "1.8.0_111" 3 …

jconsole连接失败:是否重试?

背景 在学习使用jconsole的时候&#xff0c;按照大佬们的教程一步一步操作的&#xff0c;到了连接的时候却一直报这个错误&#xff0c;连接不上远程服务&#xff0c;百度了很久也没有相关的说明&#xff0c;都是让添加那些启动参数&#xff0c;问题是启动参数我早就添加了。。…

JConsole详解

一、JConsole是什么 从Java 5开始 引入了 JConsole。JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行。您可以轻松地使用 JConsole(或者,它更高端的 “近亲” VisualVM )来监控 Java 应用程序性能和跟踪 Java 中的代码。 二、如何启动JConsole 如…

Jconsole小记

今天在看jmeter相关的文章时&#xff0c;看到了这个&#xff0c;网上查看了一些相关的博客&#xff0c;虽然现在还不做性能方面的&#xff0c;先记录下来以便日后查看。 Jconsole&#xff0c;Java Monitoring and Management Console。 Jconsole是JDK自带的监控工具&#xff…

jconsole使用

目的&#xff1a; 为了检查一个经常会跑死的java程序&#xff0c;在网上查询了下&#xff0c; 觉得jconsole这个工具还不错&#xff0c;所以就部署了一把 一、服务器端部署 首先在服务器端&#xff0c;启动你要监控的java程序&#xff0c;我的启动脚本如下&#xff1a; /ho…