当前位置:网站首页>异常: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")));
}
边栏推荐
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- Lean product development - Lean Software Development & lean product development
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- IText 7 generate PDF summary
- (the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
- 平衡二叉树详解 通俗易懂
- Database table splitting strategy
- [GNSS data processing] Helmert variance component estimation analysis and code implementation
- Unity scene jump and exit
猜你喜欢
2022国赛Re1 baby_tree
Fabrication of fairygui simple Backpack
服务未正常关闭导致端口被占用
What are the advantages of using SQL in Excel VBA
First use of dosbox
In 2020, the average salary of IT industry exceeded 170000, ranking first
FairyGUI增益BUFF數值改變的顯示
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Teach you to release a DeNO module hand in hand
Unity3D制作注册登录界面,并实现场景跳转
随机推荐
平衡二叉树详解 通俗易懂
What are the functions and features of helm or terrain
FairyGUI循環列錶
Fairygui loop list
FairyGUI簡單背包的制作
[leetcode622] design circular queue
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Theoretical derivation of support vector machine
wsl常用命令
Devops' future: six trends in 2022 and beyond
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Lean product development - Lean Software Development & lean product development
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
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
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
KF UD分解之UD分解基础篇【1】
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字