【Java】文件流 BUG

阅读: 评论:0

【Java】文件流 BUG

【Java】文件流 BUG

一、问题

1.1、环境
电脑环境:Windows 10;
开发工具:IntelliJ IDEA;
数据库环境:Redis 3.2.100
JDK环境: Jdk1.8;

1.2、问题
Check the return value of the “read” call to see how many bytes were read.

二、解答

1、源代码

try {String fileName = deFileName(templateName, request);URL fileUrl = new URL(tempFileUrl);HttpURLConnection httpUrl = (HttpURLConnection) fileUrl.openConnection();t();// 以流的形式下载文件。InputStream fis = new InputStream());byte[] buffer = new byte[fis.available()];ad(buffer);fis.close();
}catch (Exception e){(e.getMessage(), e);
}

这段代码的问题在哪里?
我们先来看看 read(byte[] by)这个方法的定义:

/*** Reads some number of bytes from the input stream and stores them into* the buffer array <code>b</code>. The number of bytes actually read is* returned as an integer.  This method blocks until input data is* available, end of file is detected, or an exception is thrown.** <p> If the length of <code>b</code> is zero, then no bytes are read and* <code>0</code> is returned; otherwise, there is an attempt to read at* least one byte. If no byte is available because the stream is at the* end of the file, the value <code>-1</code> is returned; otherwise, at* least one byte is read and stored into <code>b</code>.** <p> The first byte read is stored into element <code>b[0]</code>, the* next one into <code>b[1]</code>, and so on. The number of bytes read is,* at most, equal to the length of <code>b</code>. Let <i>k</i> be the* number of bytes actually read; these bytes will be stored in elements* <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,* leaving elements <code>b[</code><i>k</i><code>]</code> through* <code>b[b.length-1]</code> unaffected.** <p> The <code>read(b)</code> method for class <code>InputStream</code>* has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>** @param      b   the buffer into which the data is read.* @return     the total number of bytes read into the buffer, or*             <code>-1</code> if there is no more data because the end of*             the stream has been reached.* @exception  IOException  If the first byte cannot be read for any reason* other than the end of the file, if the input stream has been closed, or* if some other I/O error occurs.* @exception  NullPointerException  if <code>b</code> is <code>null</code>.* @see        java.io.InputStream#read(byte[], int, int)*/public int read(byte b[]) throws IOException {return read(b, 0, b.length);}

这里大致翻译一下:

1、从输入流中读取一定数量的字节,并且保存在缓存。
2、读到的返回值,是整数。
3、直到读取可读的文件到达末尾,或者异常才会终止。
4、如果数组长度是 0,则读取不到字节,返回值是 0;否则,至少读取 1 个字节。
5、流被读取到末尾时,则没有任何有效字节,返回 -1;否则,则会被正常读取,并存储到 b 数组中。
6、真正读取到的字节的长度,跟数组的长度是一致的。

所以,我们这里要判断,文件读取完毕了,再关掉输入流。修改后的代码如下:

try {URL fileUrl = new URL("tempFileName");HttpURLConnection httpUrl = (HttpURLConnection) fileUrl.openConnection();t();// 以流的形式下载文件。InputStream fis = new InputStream());byte[] buffer = new byte[fis.available()];while (true) {int read = ad(buffer);//判断是不是读到了数据流的末尾 ,防止出现死循环。if (read == -1) {break;}}fis.close();
}catch (Exception e){(e.getMessage(), e);
}

完毕~

三、总结

承接项目开发(电商,金融,直播等互联网开发项目),承接外包等互联网业务~

欢迎关注我的
CSDN博客
微信公众号:幕桥社区

知乎:张牧野,
简书

本文发布于:2024-01-28 11:28:02,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064124877099.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件   Java   BUG
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23