求问一个关于IO流中的切割文件和合并文件的一些问题

a245066003 2017-09-29 09:24:21
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,说明数组内就没有数据,这是怎么回事?是路径错了吗?
...全文
150 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39912309 2017-09-30
  • 打赏
  • 举报
回复
引用 1 楼 qq_39912309 的回复:
File dir = new File("e:\\partfiles");这里是文件名,不是文件夹名
说错了,要判断是不是文件夹isDirectory()
qq_39912309 2017-09-30
  • 打赏
  • 举报
回复
File dir = new File("e:\\partfiles");这里是文件名,不是文件夹名

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧