1

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()

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.

For example it is explained here: How to read a file (e.g txt file) from another java package withouth specifying the absolute path?

I thought I could do something like

"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?

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

For example I'm seeing:

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

have listOfFiles be null.

Community
  • 1
  • 1
Tai
  • 1,116
  • 5
  • 18
  • 46
  • File can only get paths from the filesystem. What you want is to get a _resource_ from your project. Like this http://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – Sekkuar Jul 10 '15 at 22:09
  • On UNIX system paths that start with `/` are absolute paths, if you want to use a relative path, don't include the `/` at the start. – Titus Jul 10 '15 at 22:14
  • @Titus I tried "src/main/java/resources" it fails as well – Tai Jul 10 '15 at 22:15
  • @Sekkuar can you explain how I might use resource to get a list off all the files? Can I do it in the same way as I was above? – Tai Jul 10 '15 at 22:15
  • I'm not sure... but I found this http://stackoverflow.com/questions/676097/java-resource-as-file – Sekkuar Jul 10 '15 at 22:24
  • @Sekkuar ok ty. Hmmm.. – Tai Jul 10 '15 at 22:25
  • @Sekkuar I actuallly got it to work. (answer below) but i'm still not sure why it works while src doesnt. – Tai Jul 10 '15 at 22:37
  • *"I want to read all of the files from within my package without using my entire file path."* Application resources will become embedded resources (in a Jar) by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. – Andrew Thompson Jul 11 '15 at 09:32
  • @AndrewThompson Sorry, could you explain that? As in I should access via what would be compiled? – Tai Jul 13 '15 at 08:19

2 Answers2

2

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(); 

They all fail.

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

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

Tai
  • 1,116
  • 5
  • 18
  • 46
  • Well, you are using an IDE right? Your compiled code probably is moved and executed in a different folder than the src/main – Sekkuar Jul 10 '15 at 22:39
  • mmm interesting. Thank you. I didn't realize that it would be compiled elsewhere. I must read up on that – Tai Jul 10 '15 at 22:46
1

This worked for me with eclipse running on Windows OS:

System.out.println(new File("src/").listFiles().length); // printing the no. of files for checking.

So, for you it would be: "src/main/java/resources/".

Tom
  • 14,120
  • 16
  • 41
  • 47
Rahul Prasad
  • 737
  • 1
  • 9
  • 13
  • I'm seeing File folder = new File("src/"); File[] listOfFiles = folder.listFiles(); where listOfFiles is null – Tai Jul 10 '15 at 22:22