当前位置:网站首页>异常: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")));
}
边栏推荐
- FairyGUI循環列錶
- Knowledge system of digital IT practitioners | software development methods -- agile
- 【无标题】
- Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
- Fabrication d'un sac à dos simple fairygui
- MySQL shutdown is slow
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- Unity3d makes the registration login interface and realizes the scene jump
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- isEmpty 和 isBlank 的用法区别
猜你喜欢
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
Office提示您的许可证不是正版弹框解决
[GNSS data processing] Helmert variance component estimation analysis and code implementation
地球围绕太阳转
FairyGUI按钮动效的混用
FairyGUI循环列表
随机推荐
[untitled]
Teach you to release a DeNO module hand in hand
IText 7 generate PDF summary
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
FairyGUI人物状态弹窗
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
Database table splitting strategy
Introduction to the daily practice column of the Blue Bridge Cup
[leetcode19] delete the penultimate node in the linked list
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
[GNSS data processing] Helmert variance component estimation analysis and code implementation
【GNSS】抗差估计(稳健估计)原理及程序实现
Unity场景跳转及退出
Lean product development - Lean Software Development & lean product development
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
FairyGUI复选框与进度条的组合使用