zoukankan      html  css  js  c++  java
  • java.nio.file.InvalidPathException: Illegal char <:>;ContextClassLoader加载jar包文件

    一、报错:

    java.nio.file.InvalidPathException: Illegal char <:>
    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)

    二、报错code

    
    
    String path = Thread.currentThread().getContextClassLoader().getResource("fileName").getFile();
    //或者
    //Thread.currentThread().getContextClassLoader().getResource("fileName").getPath();

    new
    String(Files.readAllBytes(Paths.get(path)), "utf-8");

    原因:windows系统读取的路径,盘符前有根路径"/"符号。

    URL url = Thread.currentThread().getContextClassLoader().getResource("fileName");
    content = new String(Files.readAllBytes(Paths.get(url.toURI())), "utf-8");

    解决思路:获取URL转URI完事,更直接点,getResourceAsStream读取完事。

    java.lang.ClassLoader
        public InputStream getResourceAsStream(String name) {
            URL url = getResource(name);
            try {
                return url != null ? url.openStream() : null;
            } catch (IOException e) {
                return null;
            }
        }
    
        public URL getResource(String name) {
            URL url;
            if (parent != null) {
                url = parent.getResource(name);
            } else {
                url = getBootstrapResource(name);
            }
            if (url == null) {
                url = findResource(name);
            }
            return url;
        }

    java.nio.file.Paths

        public static Path get(URI uri) {
            String scheme =  uri.getScheme();
            if (scheme == null)
                throw new IllegalArgumentException("Missing scheme");
    
            // check for default provider to avoid loading of installed providers
            if (scheme.equalsIgnoreCase("file"))
                return FileSystems.getDefault().provider().getPath(uri);
    
            // try to find provider
            for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
                if (provider.getScheme().equalsIgnoreCase(scheme)) {
                    return provider.getPath(uri);
                }
            }
    
            throw new FileSystemNotFoundException("Provider "" + scheme + "" not installed");
        }
    
    
    
        public static Path get(String first, String... more) {
            return FileSystems.getDefault().getPath(first, more);
        }

    三、Exception

    //jar程序读取报错 java -jar
    java.nio.file.FileSystemNotFoundException
        at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
        at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
        at java.nio.file.Paths.get(Paths.java:143)

    //jar读取文件报错,找不到文件
    //URL url = Thread.currentThread().getContextClassLoader().getResource(filename);
    //String content = new String(Files.readAllBytes(Paths.get(url.toURI())), "utf-8");

    //直接通过流读取文件内容
    public static String getFileContent(String filename) {
    InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    String content = null;
    try {

    int size = 1024;
    byte[] buffer = new byte[size];
    int readLen = 0;
    while ((readLen = inStream.read(buffer, 0, size)) != -1) {
    bos.write(buffer, 0, readLen);
    }

    byte[] bytes = bos.toByteArray();
    content = new String(bytes);

    } catch (IOException e) {
    log.error("读取文件内容出错", e);
    } finally {
    try {
    if(inStream != null)
    inStream.close();
    if(bos != null)
    bos.close();
    } catch (IOException e) {
    ;
    }
    }

    return content;
    }
    获取resource目录的路径
    Thread.currentThread().getContextClassLoader().getResource("").getPath()

    Maven获取resources的文件路径、读取resources的文件

     https://blog.csdn.net/huanghaocs/article/details/83242628

    Java项目读取resources资源文件路径那点事

  • 相关阅读:
    题解 P1587 【[NOI2016]循环之美】
    PKUSC2019颓废记
    使用ImageIO.write上传二维码文件时候,提示系统找不到指定路径
    rt.jar包添加源文件只需要关联到已安装对应jdk目录下source.zip源码文件即可
    Kali Linux安装中文输入法
    性能测试(一)——理发店模型
    瑜伽,不仅仅是瑜伽,敬艾扬格大师
    为什么想做测试,我的测试开端
    责任链模式-Chain of responsibility
    后缀数组-基础
  • 原文地址:https://www.cnblogs.com/foolash/p/12180455.html
Copyright © 2011-2022 走看看