当前位置:网站首页>异常: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")));
}
边栏推荐
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- KF UD分解之UD分解基础篇【1】
- Itext 7 生成PDF总结
- Unity3d, Alibaba cloud server, platform configuration
- Containers and Devops: container based Devops delivery pipeline
- Acwing-116 pilot brother
- [untitled]
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
- Matlab读取GNSS 观测值o文件代码示例
猜你喜欢
Naive Bayesian theory derivation
(core focus of software engineering review) Chapter V detailed design exercises
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
FairyGUI条子家族(滚动条,滑动条,进度条)
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
FairyGUI摇杆
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
2021.11.10 compilation examination
KF UD分解之UD分解基础篇【1】
Unity3D制作注册登录界面,并实现场景跳转
随机推荐
Basic DOS commands
MySQL shutdown is slow
IText 7 generate PDF summary
Idea problem record
基本Dos命令
Fabrication of fairygui simple Backpack
rtklib单点定位spp使用抗差估计遇到的问题及解决
What are the advantages of using SQL in Excel VBA
地球围绕太阳转
FairyGUI按钮动效的混用
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
FairyGUI人物状态弹窗
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
Prove the time complexity of heap sorting
Unity3D,阿里云服务器,平台配置
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
编辑距离(多源BFS)
FairyGUI摇杆
雇佣收银员【差分约束】
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字