java - 文件合并 ,输出的是时候拒绝访问
迷茫
迷茫 2017-04-17 17:40:58
[Java讨论组]

[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)

__

迷茫
迷茫

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

全部回复(3)
迷茫
/**
 * Creates a file output stream to write to the file with the
 * specified name. A new <code>FileDescriptor</code> object is
 * created to represent this file connection.
 * <p>
 * First, if there is a security manager, its <code>checkWrite</code>
 * method is called with <code>name</code> as its argument.
 * <p>
 * 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 <code>FileNotFoundException</code> 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
 *               <code>checkWrite</code> 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";// 输出路径
PHP中文网

试试这个.

String endpath="G://sqlit";

File.separator == "/"

PHP中文网

提一个BUG

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

这个地方是不是缺少了return

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号