-
Finding file/path anywhere on classpath...
I have a program that needs to find a file in an "earlier" directory. I know the code below is close, but there is still something wrong, but I dont' know what. Any help?
<pre>
InputStream is = PKI.class.getClass().getResourceAsStream("/rsaPUB.txt");
this will get rsaPUB.txt if it's in the same directory but not in directories "above" it
InputStream is = PKI.class.getClass().getResourceAsStream("rsaPUB.txt");
won't get anything, it's that slash that helps, but still, i can't get anything above...
</pre>
-
You'll wanna change "/rsaPUB.txt" to ""/../rsaPUB.txt". Though you'll find it's better to make programs more dynamic in case situations change.
Hope that helps.
Meethoss
-
You'll wanna change "/rsaPUB.txt" to ""/../rsaPUB.txt". Though you'll find it's better to make programs more dynamic in case situations change.
Hope that helps.
Meethoss
-
Wouldn't that just start the search in the above directory?
-
Yep, that's what I assumed you meant by "earlier" directory. If not, then please ellaborate. Thanks.
Meethoss
-
What I ment by "earlier" was just an undetermined number of directories before the one the program is in.
-
So you wanna search through all previous directories to see if the file is there? Hmm...Bit rusty on this sort of stuff so I'll look it up and see if I can find anything.
Meethoss
-
i would suggest you create a recursive method for this. ( is this the right term - recursion? ) anyway.... get the full path of where current program is currently residing, then just search through the parent directories starting at the "root".
public String search( String path )
{
.....
if( ....isDirectory() ){
search( ........... ); }
else{
file.exists(); }
return ....
}
of course you'll have to get the starting directory e.g. c:\projects\myprogram\temp. dissect the path so you start with the 'projects' folder. then the method will do the rest. you have to elaborate on the example method above....you'll have to know where the file was found and mark it.
-
Originally posted by xylex_blaiste
i would suggest you create a recursive method for this. ( is this the right term - recursion? )
Yeah, it's recursion And I agree with the explaination. I should PROBABLY know how to do this as I have a second year Uni Java exam on Monday - heh! Nevermind, still 2 more days (including today) of revision :P
Meethoss
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks