当前位置:网站首页>异常: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")));
}
边栏推荐
- Unity3D制作注册登录界面,并实现场景跳转
- What are the functions and features of helm or terrain
- (core focus of software engineering review) Chapter V detailed design exercises
- First use of dosbox
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- GPS高程拟合抗差中误差的求取代码实现
- InnoDB dirty page refresh mechanism checkpoint in MySQL
- [offer9] implement queues with two stacks
- [algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
- 2022国赛Re1 baby_tree
猜你喜欢

FairyGUI条子家族(滚动条,滑动条,进度条)

C programming exercise

Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)

Naive Bayesian theory derivation
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组

Fabrication of fairygui simple Backpack

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

FairyGUI增益BUFF數值改變的顯示

Programming homework: educational administration management system (C language)

Easy to use shortcut keys in idea
随机推荐
FairyGUI簡單背包的制作
Office提示您的许可证不是正版弹框解决
Matlab读取GNSS 观测值o文件代码示例
Containers and Devops: container based Devops delivery pipeline
【干货】提升RTK模糊度固定率的建议之周跳探测
FairyGUI摇杆
Pride-pppar source code analysis
Itext 7 生成PDF总结
微信小程序开发心得
[offer18] delete the node of the linked list
Game 280 weekly
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
Special palindromes of daily practice of Blue Bridge Cup
Wechat applet development experience
服务未正常关闭导致端口被占用
燕山大学校园网自动登录问题解决方案
音乐播放(Toggle && PlayerPrefs)
FGUI工程打包发布&导入Unity&将UI显示出来的方式