简体   繁体   English

尝试从资源文件夹中读取 txt 文件时出现 Java FileNotFoundException

[英]Java FileNotFoundException when trying to read txt file from resources folder

I'm trying to read a text file located in src/main/resources/test/file.txt .我正在尝试读取位于src/main/resources/test/file.txt的文本文件。 I'm trying to get the path of the file using String path = getClass().getResource("/text/file.txt").getFile();我正在尝试使用String path = getClass().getResource("/text/file.txt").getFile();获取文件的路径but when I try to read it I get a FileNotFoundException.但是当我尝试阅读它时,我得到了 FileNotFoundException。 I tried putting many different paths, all of which failed.我尝试了许多不同的路径,但都失败了。 How can I go about doing this?我怎么能 go 关于这样做?

Try giving complete path of the file from the disk.尝试从磁盘给出文件的完整路径。

C:\Users\MyUser\Desktop\file name with extension C:\Users\MyUser\Desktop\带扩展名的文件名

The idea of putting something into the src/main/resources tree is that it will be copied into the JAR file that you build from your project.将某些内容放入 src/main/resources 树的想法是将其复制到您从项目构建的 JAR 文件中。 It will then be available to your application via the Class methods getResource(String) and getResourceAsStream(String) methods.然后它将通过Class方法getResource(String)getResourceAsStream(String)方法对您的应用程序可用。

When you are running in your application in the development environment, it is certainly possible to use FileInputStream etcetera to access the resource.当您在开发环境中运行应用程序时,当然可以使用FileInputStream等来访问资源。 But this won't work in production.但这在生产中不起作用。 In production, the resources will then be inside your app's JAR file.在生产环境中,资源将位于您应用的 JAR 文件中。 FileInputStream cannot open a JAR file and its contents by name . FileInputStream无法按名称打开 JAR 文件及其内容。

When you do this:当你这样做时:

 getClass().getResource("/text/file.txt");

you get a URL for the resource, which will look something like this:你会得到一个资源的 URL,它看起来像这样:

 jar:file:/path/to/your.jar!/text/file.txt"

It is not possible to turn that into a pathname the FileInputStream will understand.不可能将其转换为FileInputStream可以理解的路径名。 Whatever you try will give you a FileNotFoundException ... or something that is not the resource you want to read.无论您尝试什么,都会给您一个FileNotFoundException ... 或者不是您想要阅读的资源的东西。

So what to do?那么该怎么办?

You have a few options, depending on your application's requirements.您有几个选项,具体取决于您的应用程序的要求。

  1. You can use getResourceAsStream and use the resulting input stream directly.您可以使用getResourceAsStream并直接使用生成的输入 stream。

  2. You can copy the contents of getResourceAsStream to a temporary file, and then use the pathname of the temporary file.您可以将getResourceAsStream的内容复制到临时文件中,然后使用临时文件的路径名。

  3. You can create an application specific directory (eg in the user's home directory) and extract the file you need from the JAR into the directory.您可以创建一个特定于应用程序的目录(例如,在用户的主目录中)并将您需要的文件从 JAR 提取到该目录中。 You might do this the first time the application runs.您可以在应用程序第一次运行时执行此操作。

  4. You could open the JAR file as a JarFile and use that API to open an InputStream for the resource.您可以将 JAR 文件作为JarFile打开,并使用该 API 打开资源的InputStream But this assumes that that the resources are in a JAR... and on some platforms (eg Windows) you may encounter problems with file locking.但这假设资源位于 JAR... 并且在某些平台(例如 Windows)上,您可能会遇到文件锁定问题。 (And it would be a bad idea to attempt to update the resource in the JAR.) (尝试更新JAR 中的资源是个坏主意。)

暂无
暂无

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

相关问题 尝试读取java中的.txt文件时,无法将FileNotFoundException解析为类型 - FileNotFoundException cannot be resolved to a type when trying to read a .txt file in java Spring boot:从 jar 读取时尝试读取资源文件夹中的目录内容会产生 FileNotFoundException - Spring boot: trying to read directory content inside resources folder produces FileNotFoundException when reading from jar Maven:尝试使用Intellij或通过jar使用Java命令从资源中打开文件时,java.io.FileNotFoundException - maven: java.io.FileNotFoundException when trying to open file from resources with Intellij or using java command with jar 尝试从资源文件夹中读取文件时出现 NullPointerException - NullPointerException when trying to read a file from resources folder 读取资源文件夹中的文件时出现 FileNotFoundException - FileNotFoundException when reading file in resources folder Java:尝试从文本文件读取时获得FileNotFoundException,即使该文件存在 - Java: Getting FileNotFoundException when trying to read from a text file even though the file exists 尝试读取文件时获取java.io.FileNotFoundException - Getting java.io.FileNotFoundException when trying to read a file java.io.FileNotFoundException:尝试读取 excel 文件时 - java.io.FileNotFoundException: when trying to read excel file 从子项目Spring的资源文件夹中读取txt文件 - Read txt file from resources folder in sub project Spring 尝试从Android Studio中的JSON文件读取时出现“ FileNotFoundException” - “FileNotFoundException” when trying to read from JSON file in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM