简体   繁体   English

Eclipse 无法读取 src 文件夹中的文本文件

[英]Eclipse can't read text file in src folder

I am implementing a Branch Predictor for one of my classes and I am trying to read files from my src folder in Eclipse but for some reason it is not able to open the files.我正在为我的一个类实现一个分支预测器,我试图从 Eclipse 中的 src 文件夹中读取文件,但由于某种原因它无法打开文件。 I have done this before with the exact same process so I'm not sure what is different.我之前使用完全相同的过程完成了此操作,因此我不确定有什么不同。

traceFile is set from the command line and if I print "input", it will print out the correct file path and I have confirmed it is there manually. traceFile 是从命令行设置的,如果我打印“输入”,它将打印出正确的文件路径,我已经手动确认它在那里。

ClassLoader loader = BiModalPredictor.class.getClassLoader();
File input = new File(loader.getResource(traceFile).getFile());
Scanner fin = new Scanner(input);

Is there any insight as to why this might be happening?有没有关于为什么会发生这种情况的见解? I've tried restarting Eclipse, refreshing the files, and I've also tested it on another program which worked.我尝试重新启动 Eclipse,刷新文件,并且还在另一个有效的程序上对其进行了测试。 No idea why it can't find this file.不知道为什么找不到这个文件。

Resources on the classpath, ie available through the classloaders getResource method, will not be files on the file system when your application is deployed as a jar file, or deployed in general.当您的应用程序部署为 jar 文件或一般部署时,类路径上的资源,即通过类加载器的getResource方法可用,将不是文件系统上的文件。 Do not use File with such resources, instead use getResourceAsStream to access the resource content.不要将File与此类资源一起使用,而是使用getResourceAsStream来访问资源内容。

Besides, your code is wrong.此外,您的代码是错误的。 getResource() returns a URL . getResource()返回一个URL If you want a File object from a URL , you should use new File(uri) , where the URI is obtained by calling url.toURI() .如果您想要来自URLFile对象,您应该使用new File(uri) ,其中URI通过调用url.toURI()

File input = new File(loader.getResource(traceFile).toURI());

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

相关问题 无法在Java Eclipse中的src文件夹之外读取html文件 - Unable to read html file outside src folder in java eclipse 我直接在src文件夹中添加了一个库,Eclipse似乎可以正确编译,但是找不到类文件? - I added a library directly to my src folder, Eclipse seems to be compiling properly, but can't find the class file? Eclipse无法将文件保存在webapps文件夹中 - Eclipse can't save file in webapps folder 从 Eclipse Src 文件夹中读取文件 - Reading a File from Eclipse Src Folder 在Mac上的Eclipse src文件夹中图像文件的路径 - Path for image file in Eclipse src folder, on a Mac 无法从手机内部存储器中的数据文件夹或文档文件夹中读取文本文件 - Can't read text file from the data folder or documents folder in the phone's internal storage 如何使用外部NON文本文件从Eclipse导出可运行Jar。 加密和解密src文件夹中的证书 - How to Export Runnable Jar from Eclipse with external NON text file. Encrypt & Decrypt Certificates in src folder 在src文件夹中获取txt文件的路径,这样我就可以将其读取到数组中 - Getting the path of a txt file in a src folder so i can read from it into an array Eclipse在我的src文件夹中看不到任何内容 - Eclipse doesn't see anything in my src folder JSP Maven 项目不会在 Eclipse 中包含 src 文件夹 - JSP Maven project won't include src folder in Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM