简体   繁体   English

找不到指定的文件?

[英]Could not find the file specified?

I get a really annoying error, code is fine but it can't find the files. 我收到一个非常烦人的错误,代码很好,但是找不到文件。

Output:
.\res\shadersbasicVertex.vs (The system cannot find the file specified) <-- yes it does actually say '\shadersbasicVertex.vs'

heres where I load the resources (or where I specify the path) 我在哪里加载资源(或指定路径)

    shaderReader = new BufferedReader(new FileReader("./res/shaders" + fileName));

libraries im using: lwjgl 我正在使用的库:lwjgl

at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at com.base.engine.ResourceLoader.loadShader(ResourceLoader.java:15)
at com.base.engine.Game.<init>(Game.java:20)
at com.base.engine.MainComponent.<init>(MainComponent.java:20)
at com.base.engine.MainComponent.main(MainComponent.java:124)

my shader folders are located at: C:\\Users\\Badfitz66\\workspace\\Rain\\Game engine\\res\\shaders 我的着色器文件夹位于:C:\\ Users \\ Badfitz66 \\ workspace \\ Rain \\ Game engine \\ res \\ shaders

Well yes, presumably fileName is "basicVertex.vs" . 好吧,大概fileName"basicVertex.vs" Put that on the end of "./res/shaders" and you'll get .\\res\\shadersbasicVertex.vs . 将其放在"./res/shaders"的末尾,您将获得.\\res\\shadersbasicVertex.vs You need an extra slash: 您需要一个额外的斜杠:

shaderReader = new BufferedReader(new FileReader("./res/shaders/" + fileName));

Or you could use the File API to resolve the path: 或者,您可以使用File API来解析路径:

File file = new File(new File("res", "shaders"), fileName);
shaderReader = new BufferedReader(new FileReader(file));

Or better yet: 或者更好:

shaderReader = Files.newBufferedReader(Paths.get("res", "shaders", fileName));

Note that: 注意:

  • This has nothing to do with OpenGL or anything 3d - you're just opening a file 这与OpenGL或任何3d无关-您只是打开一个文件
  • FileReader always uses the platform default encoding; FileReader 始终使用平台默认编码。 I would recommend using an approach which allows you to specify the encoding - the last example does, but defaults to UTF-8. 我建议您使用一种允许您指定编码的方法-最后一个示例可以,但是默认为UTF-8。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM