线程“main”中的异常 java.nio.file.InvalidPathException: Illegal char <:> at index 2:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43972777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:15:24  来源:igfitidea点击:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2:

javanio

提问by Jay Smith

I have to copy classpath resource from one package to another.

我必须将类路径资源从一个包复制到另一个包。

My program is:

我的程序是:

    public static void main(String[] args) throws IOException, URISyntaxException {

            ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader();
InputStream in = classLoader.getResourceAsStream("com/stackoverflow/main/Movie.class");

            URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI();
            Path path = Paths.get(uri.getPath(),"Movie.class");
            System.out.println(path);

            long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
            System.out.println(copy);

        }

At Files.copymethod I get exception:

Files.copy方法我得到异常:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/Programs/workspaceEE/HibernateDemo/target/classes/com/stackoverflow/json
    at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)
    at com.stackoverflow.main.CopyFileToDirectoryTest.main(CopyFileToDirectoryTest.java:34)

How to solve it?

如何解决?

Solution

解决方案

public static void main(String[] args) throws IOException, URISyntaxException {
        ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader();
        InputStream in = classLoader.getResourceAsStream("com//stackoverflow//main//Movie.class");
        URI uri = ClassLoader.getSystemResource("com//stackoverflow//json").toURI();
        String mainPath = Paths.get(uri).toString();
        Path path = Paths.get(mainPath, "Movie.class");
        System.out.println(path);
        long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
        System.out.println(copy);
    }

This code correctly copies Movie.classfrom package com/stackoverflow/maininto com/stackoverflow/json.

此代码正确地Movie.class从包复制com/stackoverflow/maincom/stackoverflow/json.

采纳答案by hunter

problem is that Paths.get()doesnt expect that kind of value which is generated from uri.getPath().

问题是Paths.get()不期望从uri.getPath().

Solution:

解决方案:

URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI();
String mainPath = Paths.get(uri).toString();
Path path = Paths.get(mainPath ,"Movie.class");

回答by techguy

I had the same issue and got the exception, noticed there was a space in the filename, so I had to trim it. After that, the issue is resolved.

我遇到了同样的问题并得到了异常,注意到文件名中有一个空格,所以我不得不修剪它。之后,问题就解决了。

Path filePath = Paths.get(dirPathStr, newFileName.trim());

回答by SUDIP SINGH

I have the same problem which I was facing from the past two days and finally, I got it Space causes such problem try to solve

我遇到了过去两天遇到的同样问题,最后,我明白了空间导致此类问题尝试解决

var fileName=YourFileName.trim();
Path filePath = Paths.get(dirPathStr, fileName);

回答by Fego

Try this:

尝试这个:

Path path = new File(getClass().getResource("/<path to the image in your build/classes folder>").getFile()).toPath();

Path path = new File(getClass().getResource("/<path to the image in your build/classes folder>").getFile()).toPath();

to get the correct path. Worked for me after several hours trying to find out why I couldn't get the file from the jar. THis works for NetBeans 8.02

以获得正确的路径。几个小时后为我工作,试图找出为什么我无法从 jar 中获取文件。这适用于 NetBeans 8.02