如何在java中从资源文件夹中读取文件?
你好:我在网上没有发现任何有用的东西,所以我问了一个问题。如何从资源文件夹中读取文件? 我有一个源文件夹(src)和一个资源文件夹(res)。两者都在Java中指定为源文件夹。我尝试使用:如何在java中从资源文件夹中读取文件?,java,Java,你好:我在网上没有发现任何有用的东西,所以我问了一个问题。如何从资源文件夹中读取文件? 我有一个源文件夹(src)和一个资源文件夹(res)。两者都在Java中指定为源文件夹。我尝试使用:新文件(Main.class.getClassLoader().getResource(“shader.glsl”).getFile()) 但它不起作用。请不要太严格,问问我是否忘了要提的东西 以下是我得到的错误: java.io.FileNotFoundException: shader.glsl (Das
新文件(Main.class.getClassLoader().getResource(“shader.glsl”).getFile())代码>
但它不起作用。请不要太严格,问问我是否忘了要提的东西
以下是我得到的错误:
java.io.FileNotFoundException: shader.glsl (Das System kann die angegebene Datei nicht finden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at com.leocat.game.shader.ShaderProgram.loadShader(ShaderProgram.java:54)
at com.leocat.game.shader.ShaderProgram.init(ShaderProgram.java:18)
at com.leocat.game.shader.StaticShader.init(StaticShader.java:8)
at com.leocat.game.states.GameState.init(GameState.java:29)
at com.leocat.game.Game.init(Game.java:91)
at com.leocat.game.Game.updateGLFW(Game.java:60)
at com.leocat.game.Game.run(Game.java:132)
at java.lang.Thread.run(Unknown Source)
java.io.FileNotFoundException:shader.glsl
位于java.io.FileInputStream.open0(本机方法)
位于java.io.FileInputStream.open(未知源代码)
位于java.io.FileInputStream。(未知源)
位于java.io.FileReader。(未知源)
位于com.leocat.game.shader.ShaderProgram.loadShader(ShaderProgram.java:54)
位于com.leocat.game.shader.ShaderProgram.init(ShaderProgram.java:18)
位于com.leocat.game.shader.StaticShader.init(StaticShader.java:8)
位于com.leocat.game.states.GameState.init(GameState.java:29)
位于com.leocat.game.game.init(game.java:91)
位于com.leocat.game.game.updateGLFW(game.java:60)
在com.leocat.game.game.run(game.java:132)
位于java.lang.Thread.run(未知源)
提前谢谢 如果您的资源文件夹中有shader.glsl
文件。也就是说,当您构建jar文件时,它将复制到jar根目录中。然后您可以将其读取为流:
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream in = classloader.getResourceAsStream("/shader.glsl");
不要使用File
API来获取资源文件,因为它可能在IDE开发期间工作(因为所有编译的文件都放在光盘上),并且在构建和部署JAR之后也不会工作(因为本机文件系统无法访问JAR内容)
直接获取资源流而不是获取文件。使用文件API,您必须在某个时刻获取FileInputStream
。因此,要使它在任何地方都能工作,请使用
InputStream in = Main.class.getClassLoader().getResourceAsStream("shader.glsl");
在这种情况下,shader.glsl
必须出现在resource
文件夹中(如果您使用的是maven),或者更精确、更通用,它必须编译到JAR文件的根目录中。如果它嵌套在JAR中,则必须使用/path/to/resource/shader.glsl
获取该资源,以便从类路径访问资源
new ClassPathResource("data/employees.dat", this.getClass().getClassLoader());
new ClassPathResource("../../../data/employees.dat", Example.class).getFile();
使用getResource检索您的资源:
resourceLoader.getResource("classpath:data/employees.dat");
按应用程序上下文
context.getResource("classpath:data/employees.dat");
作为输入流读取
InputStream resource = new ClassPathResource("data/employees.dat").getInputStream();
您可以使用Guava类安全地访问资源。它可以方便地将它们作为字符串
或列表
行来读取。此外,如果配置错误,它也会失败,出现更有意义的例外情况。请提供更多信息,说明您的项目类型和您做了什么?您是否有任何错误?“但它不起作用”-请澄清您的意思?如果你能提供一个能真正帮助我们帮助你的方法。也许你可以在类ShaderProgram
中发布方法loadShader()
的代码?那不是春天吗???