简体   繁体   English

尝试读取文件时出现奇怪的NoSuchFileException

[英]Strange NoSuchFileException when trying to read a file

File privateKeyFile = new File(this.getClass().getClassLoader().getResource("privateKey").getFile());

successfully gives me a keyFile. 成功给了我一个keyFile。 If I now list the path with: 如果我现在列出以下路径:

privateKeyFile.toPath()

debug successfully shows me a path to the file: 调试成功向我显示了文件的路径:

file:/Users/me/.m2/repository/com/xx/xyz/abc/encryption/1.0/encryption-1.0.jar!/privateKey 文件:/Users/me/.m2/repository/com/xx/xyz/abc/encryption/1.0/encryption-1.0.jar!/ privateKey

-- -

However, as soon as I try and read that file with 但是,一旦我尝试读取该文件,

Files.readAllBytes(privateKeyFile.toPath())

I get 我懂了

Method threw 'java.nio.file.NoSuchFileException' exception. 方法抛出“ java.nio.file.NoSuchFileException”异常。

This is really confusing, and I've tried changing the getResource() to various things like getResource("/privateKey"); 这真的很令人困惑,我尝试将getResource()更改为诸如getResource("/privateKey");类的各种东西getResource("/privateKey"); - yet that errors a lot sooner, actually a NPE right when trying to create a new File() , so the file MUST exist as I've shown above?? -但是这个错误要早得多,实际上是尝试创建new File()时的NPE权限,因此该文件必须存在,如我上面所示?

Thanks to replies, I now use this code successfully 多亏了回复,我现在可以成功使用此代码

//working
InputStream publicKeyStream = this.getClass().getClassLoader().getResourceAsStream("publicKey");
toByteArray(privateKeyStream));

I initally tried the other method that was given, but that resulted in a BadPaddingException, likely due to not fully reading the file 我最初尝试了给定的其他方法,但是导致BadPaddingException,这可能是由于未完全读取文件导致的

//The incorrect code:

byte[] array = new byte[in.available()];
in.read(array);

The constructor of File does not care if the path string actually points to an existing file, so do not rely on that to check whether the file is there or not. File的构造函数并不关心路径字符串是否实际指向现有文件,因此请不要依赖于它来检查文件是否存在。 Use privateKeyFile.exists() instead (it returns true if the file exists). 请改用privateKeyFile.exists() (如果文件存在,则返回true )。 From what I see, the file really isn't there or the path you give isn't correct, so exists() should return false. 从我的角度来看,该文件确实不存在或您提供的路径不正确,因此exists()应该返回false。

Since the file is inside of your Jar, it is not recognized by Java as an actual "file". 由于该文件位于您的Jar内,因此Java无法将其识别为实际的“文件”。 Because of this, you have to read it a little differently. 因此,您必须阅读一些不同的内容。 According to this post , you might read it something like this: 根据这篇文章 ,您可能会读到以下内容:

InputStream in = getClass().getResourceAsStream("privatekey");

byte[] array = new byte[in.available()];
in.read(array);

Or of you're in Java 9+, it could look like this: 或者您使用的是Java 9+,可能看起来像这样:

InputStream in = getClass().getResourceAsStream("privatekey"); 
byte[] array = in.readAllBytes();

Edit: Since some people wanted an example with the entire source code of the read function, here you go: 编辑:由于有些人想要一个具有读取功能的完整源代码的示例,因此您可以执行以下操作:

InputStream in = getClass().getResourceAsStream("privatekey"); 

List<Byte> bytes = new ArrayList<Byte>();
while(in.available() > 0) {
    byte[] b = new byte[in.available()];
    in.read(b);
    bytes.addAll(b);
}

byte[] array = (byte[]) bytes.toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试下载可执行文件时出现NoSuchFileException - NoSuchFileException when trying to download an executable 运行jar文件时出现NoSuchFileException - NoSuchFileException when running jar file 尝试在 jave 中上传图像时出现 NoSuchFileException - NoSuchFileException when trying to upload image in jave 使用nio创建文件时出现NoSuchFileException - NoSuchFileException when creating a file using nio 当工作空间中存在文件时,Jenkins 抛出 NoSuchFileException - Jenkins throws NoSuchFileException when file exists in workspace NoSuchFileException即使有? Java(尝试在启动窗口上添加文件) - NoSuchFileException even though there is? Java (trying to add file on startup windows) java.nio.file.NoSuchFileException 当 File.transferTo() 被调用时 - java.nio.file.NoSuchFileException When File.transferTo() is called 创建符号链接时,ConcurrentExecution异常和nio.file.NoSuchFileException - ConcurrentExecution Exception & nio.file.NoSuchFileException when creating Symbolic Link 为什么在此文件上运行 readAllBytes 时会收到 NoSuchFileException - Why am I getting a NoSuchFileException when running readAllBytes on this file 使用java.nio.file.Files.createTempDirectory时出现NoSuchFileException - NoSuchFileException when using java.nio.file.Files.createTempDirectory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM