当前位置:网站首页>异常:IOException:Stream Closed
异常:IOException:Stream Closed
2022-07-06 09:18:00 【非风之想】
异常提示
Exception in thread “main” java.io.IOException: Stream Closed
流被关闭了。
代码如下
写的是一个把多个被切割的MP3文件合并成一个MP3文件。
public class MergeFileDemo{
public static void main(String[] args) throws IOException {
File dir=new File("gril");
mergeFile(dir);
}
public static void mergeFile(File dir) throws IOException {
//
ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
for (int i = 1; i <= 5; i++) {
al.add(new FileInputStream(new File(dir,i+".part")));
Enumeration<FileInputStream> en=Collections.enumeration(al);
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream(new File(dir,"丐帮主题.mp3"));
byte[] buf=new byte[1024];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
}
问题分析
流被关闭肯定是fos.close();和sis.close();出了问题,仔细检查发现关闭流写在了for循环里面,第一次循环时就关掉了流。而且这个for循环时将所有文件添加al.add();后就结束了,这里将后面的代码也写进去了,所以造成了异常。
解决方法
fos.close();
sis.close();
将流关闭写在for循环外面。将for循环代码块修改为下,也就是将}放到al.add();后面。
for (int i = 1; i <= 5; i++) {
al.add(new FileInputStream(new File(dir,i+".part")));
}
边栏推荐
- GPS高程拟合抗差中误差的求取代码实现
- First use of dosbox
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
- Database table splitting strategy
- FairyGUI增益BUFF數值改變的顯示
- [rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
- Unity3D制作注册登录界面,并实现场景跳转
- 编辑距离(多源BFS)
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- [算法] 劍指offer2 golang 面試題2:二進制加法
猜你喜欢
![[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity](/img/9d/7284c1399964d3fb48886f12e4941c.jpg)
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity

堆排序【手写小根堆】

Unity场景跳转及退出

The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
![[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix

Liste des boucles de l'interface graphique de défaillance

【干货】提升RTK模糊度固定率的建议之周跳探测

Fairygui joystick
![[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和

What are the advantages of using SQL in Excel VBA
随机推荐
燕山大学校园网自动登录问题解决方案
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
First use of dosbox
FairyGUI循環列錶
[899] ordered queue
Lean product development - Lean Software Development & lean product development
Basic DOS commands
GPS高程拟合抗差中误差的求取代码实现
[GNSS data processing] Helmert variance component estimation analysis and code implementation
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
编辑距离(多源BFS)
Easy to use shortcut keys in idea
【无标题】
[Chongqing Guangdong education] Shandong University College Physics reference materials
【干货】提升RTK模糊度固定率的建议之周跳探测
Excel导入,导出功能实现
Guided package method in idea
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT