java.nio.file.InvalidPathException: Illegal char <:>

一、报错:

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);
    }
获取resource目录的路径
Thread.currentThread().getContextClassLoader().getResource("").getPath()

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

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

原文地址:https://www.cnblogs.com/foolash/p/12180455.html

时间: 2024-11-05 15:14:43

java.nio.file.InvalidPathException: Illegal char <:>的相关文章

java 7 使用java.nio.file.*操作文件

java7中辅助类Files新增了两个方法用于读去文件的全部行和全部字节.So..再也不用缓冲区了. 1 package java8_test; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 import java.util.List; 8 9 public class TestMain { 10

docker elasticsearch挂载宿主机报 java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes

① docker启动 es实例 docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \ -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasti

java.nio.file.Path

public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. 2. A root

Jenkins迁移jobs后项目构建出现java.nio.file.DirectoryNotEmpt

1.首先说一下迁移过程1)新环境部署Jenkins持续集成环境,这里不在详细说明安装过程:2)直接把旧的Jenkins家目录下得jobs目录通过scp得方式进行传输到新得环境下; 迁移完成后启动新环境Jenkins服务,构建项目控制台输出如下: 观察控制台输出到最后,项目是构建成功得,而且不影响运行. 2.错误原因通过Google查找得知有人提交了这个issues,This started happening on a new Jenkins server that a teammate was

java.nio.file.FileSystemException: D:\kafka_2.12-2.1.0\kafka_2.12-2.1.0\logs\__consumer_offsets-30\00000000000000000000.timeindex.cleaned: 另一个程序正在使用此文件,进程无法访问。

在启动kafka时候报错: 原因是因为在logs中文件有问题,那么可以删除配置文件 文件夹下文件,还有就是重新建立一个文件夹 原文地址:https://www.cnblogs.com/xiufengchen/p/10960951.html

5种调优Java NIO和NIO.2的方式

Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/O密集型任务的性能.过了十年,很多Java开发者还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2).这篇教程展示了5个在Java编程的一些常见场景里使用NIO和NIO.2包的简单示例. NIO和NIO.2对于Java平台最大的贡献是提高了Java应用开发中的一个核心组件的

Java NIO 完全学习笔记(转)

本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java NIO,被称为新 IO(New IO),是 Java 1.4 引入的,用来替代 IO API的. Java NIO:Channels and Buffers 标准的 Java IO API ,你操作的对象是字节流(byte stream)或者字符流(character stream),而 NIO,你操

Five ways to maximize Java NIO and NIO.2--reference

Java NIO -- the New Input/Output API package-- was introduced with J2SE 1.4 in 2002. Java NIO's purpose was to improve the programming of I/O-intensive chores on the Java platform. A decade later, many Java programmers still don't know how to make th

Five ways to maximize Java NIO and NIO.2--转

原文地址:http://www.javaworld.com/article/2078654/java-se/java-se-five-ways-to-maximize-java-nio-and-nio-2.html Java NIO -- the New Input/Output API package-- was introduced with J2SE 1.4 in 2002. Java NIO's purpose was to improve the programming of I/O-