[toc]
package cn.qina.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.TreeSet; public class CombinationUtil { /*输入要被合并的文件所在的路径 * * */ public static void combination(String stapath,String endpath) throws IOException { File stafile=new File(stapath); if ((!stafile.exists())||(!stafile.isDirectory())) { System.out.println("请正确的输入路径"); } //排序文件,按照名字 TreeSet set=new TreeSet(new Comparator() { @Override public int compare(File o1, File o2) { return o1.getPath().compareTo(o2.getPath()); } }); File[] files=stafile.listFiles();//获取开始目录下的所有文件 ArrayList inpList=new ArrayList(); ArrayList fileList=new ArrayList(); for (int i = 0; i < files.length; i++) { if (!files[i].isDirectory()) {//过滤掉文件夹 set.add(files[i]); } } fileList.addAll(set);//传入文件列表 for (int i = 0; i < fileList.size(); i++) { inpList.add(new FileInputStream(fileList.get(i)));//转换为输入流 } SequenceInputStream s=new SequenceInputStream(Collections.enumeration(inpList)); byte[] b=new byte[1024*1024*10]; int len=0; FileOutputStream outputStream=new FileOutputStream(endpath); while ((len=s.read(b))!=-1) { outputStream.write(b, 0, len); } outputStream.close(); s.close(); } }
package cn.qina.file; import java.io.File; import java.io.IOException; public class Sqlittext { public static void main(String[] args) { String endpath="G:"+File.separator+"sqlit";//输出路径 String stapath="G:"+File.separator+"sqlit";//输入路径 try { CombinationUtil.combination(stapath, endpath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
java.io.FileNotFoundException: G:\sqlit (拒绝访问。)
at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.
(FileOutputStream.java:194) at java.io.FileOutputStream. (FileOutputStream.java:84) at cn.qina.file.CombinationUtil.combination(CombinationUtil.java:54) at cn.qina.file.Sqlittext.main(Sqlittext.java:12) __
看第三段
将
endpath
修改为一个文件即可试试这个.
File.separator == "/"
提一个BUG
这个地方是不是缺少了return