java.nio.file.InvalidPathException:索引 2 处的非法字符 <:>:

java.nio.file.InvalidPathException: Illegal char <:> at index 2:

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

我的程序是:

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

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

            URI uri = ClassLoader.getSystemResource("com/Whosebug/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);

        }

Files.copy 方法中我得到异常:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/Programs/workspaceEE/HibernateDemo/target/classes/com/Whosebug/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.Whosebug.main.CopyFileToDirectoryTest.main(CopyFileToDirectoryTest.java:34)

如何解决?

解决方案

public static void main(String[] args) throws IOException, URISyntaxException {
        ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader();
        InputStream in = classLoader.getResourceAsStream("com//Whosebug//main//Movie.class");
        URI uri = ClassLoader.getSystemResource("com//Whosebug//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);
    }

此代码正确地将 Movie.class 从包 com/Whosebug/main 复制到 com/Whosebug/json

问题是 Paths.get() 不期望从 uri.getPath() 生成的那种值。

解决方案:

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

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

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

我这两天遇到了同样的问题,终于解决了 Space 导致这样的问题 尝试解决

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

试试这个:

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

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

以下解决方案可以正常工作:

解决方案1:

String logFileLocalPath = "../log/log.txt";
File logFile = new File(getURL(logFileLocalPath).getPath());

解决方案 2:

File logFile = new 
File(Paths.get(getURL(logFileLocalPath).toURI()).toString());

private static URL getURL(String localPath) {

      return Main.class.getResource(localPath);
}

我在自动化中使用 extentReports 时遇到了同样的问题,我只是更正了报告路径

public void config() {
    String path = System.getProperty("user.dir")+"\reports\index.html";       
    ExtentSparkReporter reporter = new ExtentSparkReporter(path);
    reporter.config().setReportName("Web Automation Reports");
    reporter.config().setDocumentTitle("Web Results");

... }

经过多次尝试,这对我有用

      Path path = new File(getClass().getResource("/data.json").getFile()).toPath(); 

现在您可以随意使用您的路径,例如

        Reader reader = Files.newBufferedReader(path);