在我的程序中,我有一行代码:
Path toRead = new File(getClass().getResource("/data.txt").toString()).toPath();每当我尝试运行这个程序时,我都会得到一个错误:
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 4作为一个普通文件,它似乎运行良好,但作为一条道路,它搞砸了,有解决办法吗?
为了使用Files.copy(),我需要它作为路径。
data.txt所在的文件夹作为源文件夹添加。
发布于 2016-03-15 00:45:54
您绝不能假设从URL返回的getResource()引用的是一个文件。您应该只使用URL.openStream()。这实际上就是getResourceAsStream()所做的。
try (InputStream is = getClass().getResourceAsStream("/data.txt")) {
Files.copy(is, targetPath);
}发布于 2018-03-20 08:37:31
试试这个:
String pathToFile = getClass().getResource("/data.txt").toString()).toPath();
String pathToFile = pathToFile.replace("/C:", "C:/");
Path toRead = Paths.get(pathToFile)https://stackoverflow.com/questions/36000646
复制相似问题