使用java中的.jar程序复制目录

时间:2017-03-03 12:43:03

标签: java jar netbeans-8

我想从我的.jar文件所在的位置复制目录? 这是我试过的..但我总是得到/ home / user / 如何从我的.jar程序存在的位置复制文件?

private void copy_dir() {

    //Path sourceParentFolder = Paths.get(System.getProperty("user.dir") +        "/Project/");
   //      Path sourceParentFolder = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString());
    Path destinationParentFolder = Paths.get(System.getProperty("user.home"));

    try {
        Stream<Path> allFilesPathStream = Files.walk(sourceParentFolder);
        Consumer<? super Path> action = new Consumer<Path>() {

            @Override
            public void accept(Path t) {
                try {
                    String destinationPath = t.toString().replaceAll(sourceParentFolder.toString(), destinationParentFolder.toString());
                    Files.copy(t, Paths.get(destinationPath));
                } catch (FileAlreadyExistsException e) {
                    //TODO do acc to business needs
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        };
        allFilesPathStream.forEach(action);

    } catch (FileAlreadyExistsException e) {
        //file already exists and unable to copy
    } catch (IOException e) {
        //permission issue
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:1)

尝试

 Path destinationParentFolder = Paths.get(System.getProperty("user.dir"));

“user.dir”获取初始化应用程序的绝对路径。

“user.home”获取用户的主目录。