当前位置:网站首页>异常: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")));
}
边栏推荐
- 基本Dos命令
- Lock wait timeout exceeded try restarting transaction
- Special palindromes of daily practice of Blue Bridge Cup
- Fairygui joystick
- Detailed explanation of balanced binary tree is easy to understand
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- [algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
- 雇佣收银员【差分约束】
- [offer78] merge multiple ordered linked lists
猜你喜欢

idea中导包方法

FairyGUI簡單背包的制作

Office提示您的许可证不是正版弹框解决
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数

【无标题】

服务未正常关闭导致端口被占用
![[algorithm] sword finger offer2 golang interview question 5: maximum product of word length](/img/e0/cea31070d6365eb57013cdead4a175.png)
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length

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

341. Flatten nested list iterator

Programming homework: educational administration management system (C language)
随机推荐
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
How to reduce the shutdown time of InnoDB database?
What are the advantages of using SQL in Excel VBA
Halcon knowledge: gray_ Tophat transform and bottom cap transform
Itext 7 生成PDF总结
最短Hamilton路径 (状压DP)
染色法判定二分图
Acwing-116 pilot brother
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
KF UD分解之UD分解基础篇【1】
Combination of fairygui check box and progress bar
Special palindromes of daily practice of Blue Bridge Cup
[algorithm] sword finger offer2 golang interview question 2: binary addition
Devops' future: six trends in 2022 and beyond
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Game 280 weekly
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
第一人称视角的角色移动
Database course design: college educational administration management system (including code)
WSL common commands