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

问题描述 投票:0回答:8

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

我的程序是:

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

        }

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)

如何解决?

解决方案

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

此代码正确地将

Movie.class
从包
com/stackoverflow/main
复制到
com/stackoverflow/json
中。

java nio
8个回答
62
投票

问题是

Paths.get()
不期望从
uri.getPath()
产生的那种价值。

解决方案:

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

9
投票

试试这个:

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

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


5
投票

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

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

3
投票

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

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

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

        Reader reader = Files.newBufferedReader(path);

2
投票

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

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

0
投票

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

解决方案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);
}

0
投票

我在自动化中使用 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");

... }


0
投票

我有同样的错误

./gradlew 构建

2023-04-10T16:56:54.855+02:00 信息 10476 --- [主要] c.v.s.d.m.a.VogellaApplication:没有活动的配置文件集, 回退到 1 个默认配置文件:线程中的“默认”异常 “main”java.nio.file.InvalidPathException:索引处的尾随字符< > 27:com.vogella.spring.di.模型 在 java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191) 在 java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) 在 java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) 在 java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)

我分析了文件名和配置。 我注意到 build.gradle 文件中的“组”属性中有一个空格

group = 'com.vogella.spring.di.model '

我删除了多余的空间,构建命令运行良好。

© www.soinside.com 2019 - 2024. All rights reserved.