求问一个关于IO流中的切割文件和合并文件的一些问题
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*
* 将文件合并
*/
File dir = new File("e:\\partfiles");
mergeFile_2(dir);
}
public static void mergeFile_2(File dir) throws IOException {
// TODO Auto-generated method stub
//遍历这个配置文件
File[] files = dir.listFiles(new SuffixFilter(".properties"));
if(files.length!=1)
throw new RuntimeException(dir+",该目录下没有properties扩展名的文件,或者不唯一!");
//记录下配置文件对象
File confile = files[0];
//获取配置文件的信息。需要用Properties
Properties prop = new Properties();
//使用读取流关联文件
FileInputStream fis = new FileInputStream(confile);
//加载配置信息到prop集合里
prop.load(fis);
//获取文件的信息
String filename = prop.getProperty("filename");
int count = Integer.parseInt(prop.getProperty("partcount"));//为什么使用Integer?因为返回的是字符串,所以需要转换下
//获取该目录下的所有碎片文件
File[] partFiles = dir.listFiles(new SuffixFilter(".part"));
if(partFiles.length!=(count-1)) {
throw new RuntimeException("碎片文件个数不符合要求,个数不对,应该为"+count+"个");
}
//将碎片文件添加到集合中。
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int x = 0;x<=partFiles.length;x++) {
al.add(new FileInputStream(partFiles[x]));
}
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir,filename));
byte[] buf = new byte[1024];
int len = 0;
if((len=sis.read(buf))!=1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
我想把e盘下的partfiles文件夹里的文件合并成一个文件,是通过读取properties配置文件来获取切割文件的信息。但是运行总是报错。
Exception in thread "main" java.lang.RuntimeException: e:\partfiles,该目录下没有properties扩展名的文件,或者不唯一!
at cn.itcast.io.splitfile.MergeFileDemo.mergeFile_2(MergeFileDemo.java:32)
at cn.itcast.io.splitfile.MergeFileDemo.main(MergeFileDemo.java:23)
这个我看了好几遍路径,都是对的呀,我切割后的目录就是这个e盘下的partfiles文件夹,里面也有切割后的part文件和priperties配置文件,但是等到我合并的时候却报异常了。这是怎么回事啊?
我把异常注释后,下面的confile也是0,说明数组内就没有数据,这是怎么回事?是路径错了吗?