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

ryevplcw  于 2023-08-02  发布在  Java
关注(0)|答案(8)|浏览(222)

我必须将类路径资源从一个包复制到另一个包。
我的程序是:

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

wmtdaxz3

wmtdaxz31#

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

字符串

axkjgtzd

axkjgtzd2#

试试这个:

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

字符串
以获得正确的路径。在我花了几个小时试图找出为什么我不能从jar里得到文件后,它对我起了作用。这适用于NetBeans 8.02

bxjv4tth

bxjv4tth3#

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

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

字符串

dldeef67

dldeef674#

在尝试了很多次之后,这对我很有效

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

字符串
现在你可以随心所欲地使用你的路径了

Reader reader = Files.newBufferedReader(path);

iyr7buue

iyr7buue5#

我有同样的问题,我是从过去的两天面临的,最后,我得到了它的空间导致这样的问题试图解决

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

字符串

wpcxdonn

wpcxdonn6#

我也犯了同样的错误
./gradlew build
2023-04-10T16:56:54.855+02:00 INFO 10476 - [ main] c.v.s.d.m.a.VogellaApplication:没有活动配置文件集,回退到1个默认配置文件:“default”Exception in thread“main”java.nio.file.InvalidPathException:索引27处的尾随字符< >:com.vogella.spring.di.model at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser. java:191)at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser. java:153)at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser. java:77)at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
我分析了文件名和配置。我注意到build.gradle文件中的“group”属性中有一个空格

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

字符串
我删除了多余的空间,构建命令工作正常。

j2qf4p5b

j2qf4p5b7#

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

String logFileLocalPath = "../log/log.txt";
File logFile = new File(getURL(logFileLocalPath).getPath());

字符串
解决方案二:

File logFile = new 
File(Paths.get(getURL(logFileLocalPath).toURI()).toString());
private static URL getURL(String localPath) {

      return Main.class.getResource(localPath);
}

的数据

vd8tlhqk

vd8tlhqk8#

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

字符串
{\fnSimHei\bord1\shad1\pos(200,288)}

相关问题