无法访问java包中的文件夹 [英] Can't access a folder in java package

查看:329
本文介绍了无法访问java包中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用整个文件路径的情况下从包中读取所有文件。例如,我可以使用这样的东西来获取目录中的所有文件。

I want to read all of the files from within my package without using my entire file path. For example I can use something like this to get all of the files in a dir.

new File("/Users/me/Documents/workspace/testing/src/main/java/resources/").listFiles()

当我在intellij中有一个看起来像这样的包时:

When I have a package in intellij that looks like this:

main
- java
--com.testFile
--resources

我读到这个:从项目目录中的文件夹中读取文件。但是我希望能够在不指定整个路径的情况下执行此操作。

I read this: Read file from a folder inside the project directory. However I want to be able to do this without specifying the the whole path.

例如,这里解释:
如何从另一个java包中读取文件(例如txt文件)而不指定绝对路径?

我以为我可以做类似

src / main / java / resources但我尝试的每个组合在插入时都会失败in to:

"src/main/java/resources" but every combination I try fails when plugged in to:

new File("/Users/me/Documents/workspace/testing/src/main/java/resources/").listFiles()

我可以发誓我之前做过这样的事吗?我犯了一个愚蠢的错误吗?

I could have sworn I've done this before like this? Am I making a silly mistake?

截至目前,它无法找到该文件。它是从java文件中的文件运行 - > com.thisproject / app

As of right now it can't locate the file. It's being run from a file in a file in java -> com.thisproject/app

例如我看到:

File folder = new File("src/");
File[] listOfFiles = folder.listFiles();

将listOfFiles设为null。

have listOfFiles be null.

推荐答案

解决了...
这实际上有点奇怪。我在网上找到的所有东西都说参考src / main等...这是我的文件路径。

Solved... This was actually kind of weird. Everything I found online said to reference src/main etc... Which is my file path.

但是我可以通过以下方式访问它:

However I was able to access it through:

new file("java/resources").listFiles(); 

哪个效果很好。如果我尝试以下任何组合:

Which works perfectly. If I were to try any of the below combinations:

new file("/java/resources").listFiles(); 
new file("main/java/resources").listFiles(); 
new file("src/main/java/resources").listFiles(); 

它们都失败了。

对于系统info我正在使用intellij,Mac OS,我正在开发一个intellij创建的webapp。不确定这是否有所作为。

For system info I'm using intellij, Mac OS, I'm working on a intellij created webapp. Not sure if that makes a difference.

如果有人知道为什么src / ...不起作用,我很想知道。

If anyone knows why src/... doesn't work I'd love to know.

这篇关于无法访问java包中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆