簡體   English   中英

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

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

我必須將類路徑資源從一個 package 復制到另一個。

我的程序是:

    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從 package com/stackoverflow/main復制到com/stackoverflow/json中。

問題是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");

嘗試這個:

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

以獲得正確的路徑。 幾個小時后為我工作,試圖找出為什么我無法從 jar 中獲取文件。 這適用於 NetBeans 8.02

我遇到了同樣的問題並得到了異常,注意到文件名中有一個空格,所以我不得不修剪它。 之后,問題就解決了。

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

經過多次嘗試,這對我有用

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

現在您可以根據需要使用您的路徑,例如

        Reader reader = Files.newBufferedReader(path);

我遇到了過去兩天面臨的同樣問題,最后,我明白了空間導致此類問題嘗試解決

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

以下解決方案正常工作:

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

... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM