Finding file/path anywhere on classpath...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Finding file/path anywhere on classpath...

  1. #1
    Join Date
    Jan 2003
    Posts
    3

    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>

  2. #2
    Join Date
    Oct 2002
    Posts
    66
    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

  3. #3
    Join Date
    Oct 2002
    Posts
    66
    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

  4. #4
    Join Date
    Jan 2003
    Posts
    3
    Wouldn't that just start the search in the above directory?

  5. #5
    Join Date
    Oct 2002
    Posts
    66
    Yep, that's what I assumed you meant by "earlier" directory. If not, then please ellaborate. Thanks.
    Meethoss

  6. #6
    Join Date
    Jan 2003
    Posts
    3
    What I ment by "earlier" was just an undetermined number of directories before the one the program is in.

  7. #7
    Join Date
    Oct 2002
    Posts
    66
    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

  8. #8
    Join Date
    Nov 2002
    Posts
    138
    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.

  9. #9
    Join Date
    Oct 2002
    Posts
    66
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center