当前位置:网站首页>Exception: ioexception:stream closed
Exception: ioexception:stream closed
2022-07-06 12:59:00 【Non wind thought】
Abnormal prompt
Exception in thread “main” java.io.IOException: Stream Closed
The stream is shut down .
The code is as follows
It's written that one or more are cut MP3 Merge the files into one MP3 file .
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," Beggars' sect theme .mp3"));
byte[] buf=new byte[1024];
int len=0;
while((len=sis.read(buf))!=-1) {
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}
}
Problem analysis
The flow must be closed fos.close(); and sis.close(); Something went wrong. , After careful inspection, it is found that the closed stream is written in for Inside the loop , The flow is turned off at the first cycle . And this for Add all files when looping al.add(); It's over , The following code is also written here , So it caused an exception .
resolvent
fos.close();
sis.close();
Write the flow closure in for Outside of the loop . take for The cyclic code block is modified to the following , Also is to } Put it in al.add(); Back .
for (int i = 1; i <= 5; i++) {
al.add(new FileInputStream(new File(dir,i+".part")));
}
边栏推荐
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- (the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
- Naive Bayesian theory derivation
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
- 错误:排序与角标越界
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- 使用rtknavi进行RT-PPP测试
- Heap sort [handwritten small root heap]
猜你喜欢
What are the advantages of using SQL in Excel VBA
FairyGUI简单背包的制作
C programming exercise
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
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
Halcon knowledge: gray_ Tophat transform and bottom cap transform
Unity scene jump and exit
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
随机推荐
Unity3d, Alibaba cloud server, platform configuration
MySQL performance tuning - dirty page refresh
微信小程序开发心得
Naive Bayesian theory derivation
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
2022国赛Re1 baby_tree
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
Code example of MATLAB reading GNSS observation value o file
How to improve the deletion speed of sequential class containers?
Containers and Devops: container based Devops delivery pipeline
KF UD decomposition pseudo code implementation advanced [2]
Matlab读取GNSS 观测值o文件代码示例
Unity scene jump and exit
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Shortest Hamilton path (pressure DP)
Meanings and differences of PV, UV, IP, VV, CV
Introduction to the daily practice column of the Blue Bridge Cup
The port is occupied because the service is not shut down normally