• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving the path of a file  RSS feed

 
Charles Marvin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to retrieve the path of a file in a way where I don't explicitly reference the actual file location myself.

Currently I'm doing:


Is there any way I can specify just "file.properties" and retrieve the path for that and put that in my parameters for my FileInputStream(fileLocation)?

Note: file.properties is stored under a package named 'resources' and my java class is under a named 'java' but both of these classes are under my 'main' package. So the structure is like....

-src
--main
---java
---resources

Thanks
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see you have a directory called java; is the JVM happy with that package name?
Is this code you are writing at the command line, or via an IDE? Try moving your resources file to the directory one above src and see whether you can then use it with its simple name.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles, welcome to the Ranch!

First, do you really need a FileInputStream, or do you just need any old InputStream which lets you read the properties? Hopefully it's the latter, because what you appear to have is a resource. So what I would suggest is this:



And second, do you plan to have your program update this resource? Hopefully not, but if you do then it's a bit more complicated.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I may have misunderstood where you said your class is located. What you need to know is whether it is in the "main" package or the "java" package. I.e. does it say


or

at the top?

The path to your resource which I used is an absolute path based on the root of your structure, and I assumed that "main" was the root. The getResourceAsStream() method determines the root as the parent folder of the class's name. So if the class's name is "java.Something" then the root is "main". But if the class's name is "main.java.Something" then the root is "src" and the name of the resource is "/main/resources/file.properties".

If that's a whole lot of stuff going over your head you could just try both versions and see which one works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!