java.nio.file jar_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

publicInputStream getResourceAsStream(String name) {

URL url=getResource(name);try{return url != null ? url.openStream() : null;

}catch(IOException e) {return null;

}

}publicURL getResource(String name) {

URL url;if (parent != null) {

url=parent.getResource(name);

}else{

url=getBootstrapResource(name);

}if (url == null) {

url=findResource(name);

}returnurl;

}

java.nio.file.Paths

public staticPath 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"))returnFileSystems.getDefault().provider().getPath(uri);//try to find provider

for(FileSystemProvider provider: FileSystemProvider.installedProviders()) {if(provider.getScheme().equalsIgnoreCase(scheme)) {returnprovider.getPath(uri);

}

}throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");

}public staticPath get(String first, String... more) {returnFileSystems.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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值