[英]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.