线程“主”java.nio.file.InvalidPathException 中的异常:索引 2 处的非法字符 <:>:

Posted

技术标签:

【中文标题】线程“主”java.nio.file.InvalidPathException 中的异常:索引 2 处的非法字符 <:>:【英文标题】:Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: 【发布时间】:2017-10-13 20:12:51 【问题描述】:

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

我的程序是:

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

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

            URI uri = ClassLoader.getSystemResource("com/***/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/***/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.***.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//***//main//Movie.class");
        URI uri = ClassLoader.getSystemResource("com//***//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/***/main 复制到 com/***/json

【问题讨论】:

这不起作用,因为您的类路径由透明和不透明资源组成——例如jar中的资源。您正在尝试写入类似于jar:file:/com/***/json 的路径,这是一个无效的PathFile,但它是一个有效的URI。通常,您不能写入类路径,只能从中读取。 没有 jar 是 maven 项目 当你编译一个 Maven 项目时,它会生成一个 jar。你会如何分发你的编译代码? (即在 Java 9 之前) 这能回答你的问题吗? Java NIO file path issue 【参考方案1】:

试试这个:

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

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

【讨论】:

【参考方案2】:

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

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

【讨论】:

【参考方案3】:

前两天遇到同样的问题,终于搞定了 空间导致这样的问题 尝试解决

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

【讨论】:

【参考方案4】:

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

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

【讨论】:

【参考方案5】:

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

解决方案:

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

【讨论】:

谢谢!我在找它

以上是关于线程“主”java.nio.file.InvalidPathException 中的异常:索引 2 处的非法字符 <:>:的主要内容,如果未能解决你的问题,请参考以下文章

Android 异步操作Android 线程切换 ( 判定当前线程是否是主线程 | 子线程中执行主线程方法 | 主线程中执行子线程方法 )

子线程怎么不阻塞主线程

QT中UI主窗口如何与子线程相互传递参数

java 子线程 回调 主线程

C++怎么在主线程中使用子线程的数据? 比如说主线程中有一个数组,如何在子线程中调用这个数组

EventBus事件通信框架 ( 发送事件 | 判断发布线程是否是主线程 | 子线程切换主线程 | 主线程切换子线程 )