java - 文件合并 ,输出的是时候拒绝访问
迷茫
迷茫 2017-04-17 17:40:58
0
3
477

[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(); } }

test类

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)

__

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (3)
迷茫
/** * Creates a file output stream to write to the file with the * specified name. A new FileDescriptor object is * created to represent this file connection. * 

* First, if there is a security manager, its checkWrite * method is called with name as its argument. *

* If the file exists but is a directory rather than a regular file, does * not exist but cannot be created, or cannot be opened for any other * reason then a FileNotFoundException is thrown. * * @param name the system-dependent filename * @exception FileNotFoundException if the file exists but is a directory * rather than a regular file, does not exist but cannot * be created, or cannot be opened for any other reason * @exception SecurityException if a security manager exists and its * checkWrite method denies write access * to the file. * @see java.lang.SecurityManager#checkWrite(java.lang.String) */ public FileOutputStream(String name) throws FileNotFoundException { this(name != null ? new File(name) : null, false); }

看第三段

如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。

endpath修改为一个文件即可

String endpath = "G:" + File.separator + "sqlit" + File.separator + "out.sqlit";// 输出路径
    Ty80

    试试这个.

    String endpath="G://sqlit";

    File.separator == "/"

      洪涛

      提一个BUG

      File stafile=new File(stapath); if ((!stafile.exists())||(!stafile.isDirectory())) { System.out.println("请正确的输入路径"); }

      这个地方是不是缺少了return

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!