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资源文件路径那点事