简体   繁体   English

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

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

I have to copy classpath resource from one package to another.我必须将类路径资源从一个 package 复制到另一个。

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.copy method 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.class from package com/stackoverflow/main into com/stackoverflow/json .此代码正确地Movie.class从 package com/stackoverflow/main复制到com/stackoverflow/json中。

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");

Try this:尝试这个:

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.几个小时后为我工作,试图找出为什么我无法从 jar 中获取文件。 This works for NetBeans 8.02这适用于 NetBeans 8.02

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

After trying many times, this worked for me经过多次尝试,这对我有用

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

Now you can use your path as you wish eg现在您可以根据需要使用您的路径,例如

        Reader reader = Files.newBufferedReader(path);

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

The following solutions work correctly:以下解决方案正常工作:

Solution1:解决方案1:

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

Solution 2:解决方案2:

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

private static URL getURL(String localPath) {

      return Main.class.getResource(localPath);
}

I faced same issue while using extentReports in my automation, i simply corrected the report path我在自动化中使用 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");

... } ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.nio.file.InvalidPathException:索引 72 处的非法字符 <:> - Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 72 gradle-resources-production:mymod.main: java.nio.file.InvalidPathException: Illegal char &lt;:&gt; at index 75 - gradle-resources-production:mymod.main: java.nio.file.InvalidPathException: Illegal char <:> at index 75 遇到java.nio.file.InvalidPathException:非法char &lt;:&gt; - Encountering java.nio.file.InvalidPathException: Illegal char <:> 使用闭包编译器格式化javascript文件时,在索引7处获取到java.nio.file.InvalidPathException:非法char &lt;:&gt; - Got java.nio.file.InvalidPathException:Illegal char <:> at index 7 when formatting javascript file using closure compiler 内部错误(java.nio.file.invalidpathexception):非法字符<!--?-->在索引 34 - internal error (java.nio.file.invalidpathexception): Illegal char <?> at index 34 线程“ main”中的异常java.nio.file.InvalidPathException:UNC路径缺少主机名:/ \\ / - Exception in thread “main” java.nio.file.InvalidPathException: UNC path is missing hostname: /\/ 在尝试编译 java 文件时获取 java.nio.file.InvalidPathException: Illegal char &lt;*&gt; - Getting a java.nio.file.InvalidPathException: Illegal char <*> while trying to compile java files java.nio.file.InvalidPathException - java.nio.file.InvalidPathException java.nio.file.InvalidPathException: Illegal char &lt;"&gt; at index 0: "C:\Program Files\Git\bin\\cl.exe at java.base/sun.nio.fs.WindowsPathParser.normalize - java.nio.file.InvalidPathException: Illegal char <"> at index 0: "C:\Program Files\Git\bin\\cl.exe at java.base/sun.nio.fs.WindowsPathParser.normalize Caused by: java.nio.file.InvalidPathException: Illegal char &lt;:&gt; at index 58: com.example.project.app-mergeDebugResources-32:/values/values.xml - Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 58: com.example.project.app-mergeDebugResources-32:/values/values.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM