线程" main"中的例外情况java.nio.file.InvalidPathException:非法char<:>在索引2:

时间:2017-05-15 06:29:09

标签: java nio

我必须将classpath资源从一个包复制到另一个包。

我的节目是:

    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

6 个答案:

答案 0 :(得分:17)

问题是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");

答案 1 :(得分:3)

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

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

答案 2 :(得分:0)

尝试使用/更改//。这将解决问题。

"com/stackoverflow/main/Movie.class""com//stackoverflow//main//Movie.class"

"com/stackoverflow/json""com//stackoverflow//json"等等。

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

尝试一下:

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

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

答案 5 :(得分: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);
}