Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: FileNotFoundException!?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default FileNotFoundException!?

    Hey folks,

    Having a bit of trouble understanding what the problem is. I created a text file that is a list of about 200 or so items, arranged one item per line as such:

    item1
    item2
    item3
    ...item200

    My class file FumeTracker.class is in it's own directory along with two other directories, "dat" and "profiles". The text file is saved inside the "dat" directory.

    Here is the code that's throwing the exception:

    import java.io.*;
     
    public class FumeTracker {
     
    	public static void main(String [] args) throws IOException{
    		BufferedReader hr = null;
     
    		try{
    			hr = new BufferedReader(new FileReader("//dat//herbmasterlist.txt"));
    			String h;
    			while((h = hr.readLine()) != null){
    				System.out.println(h);
    			}
    		}finally{
    			if (hr != null)
    				hr.close();
    		}
    	}
     
    }

    The key here is that this program will eventually need to run on multiple systems (windows, mac or linux) and be extracted to whatever directory the user wants, as long as my directory structure remains in tact (ie my dat and profile directories must stay in the same parent directory that the executable will be in) so I don't want to hard code an absolute path.

    Any suggestions on how I might get this workin? Any help is appreciated.

    Thanks in advance,
    ~Shad


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: FileNotFoundException!?

    Try using single / vs //

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException!?

    Thank you for your suggestion. Unfortunately, that was not the (only?) issue

    I am using Eclipse, not sure if that matters. I am also using a Unix machine, my daughter's in fact (only relevant because the user directory is cassandra, see below lol)

    This is the working directory order

    /home/cassandra/workspace

    contains the directory FumeTracker and a copy of herbmasterlist.txt

    /home/cassandra/workspace/FumeTracker

    contains the directories bin and src

    /home/cassandra/workspace/FumeTracker/bin

    contains the directories dat and profiles, as well as a copy of herbmasterlist.txt and
    FumeTracker.class (I managed to get it to compile once by hard coding the full path)

    profiles directory is empty

    /home/cassandra/workspace/FumeTracker/src

    contains FumeTracker.java and a copy of herbmasterlist.txt


    I apologize if this is all extraneous information. There are 3 copies of my .txt file because I don't know what Java considers the home directory in a running program, so I'm not sure what the relative path should be. If it's the same directory that the class resides in, then I should be able to simply write "herbmasterlist.txt" because I have a copy in that dir as well, but that won't work either.

    If I could figure out which directory the program checks when no other directories are specified in a file name, then maybe that could clue me in as to where I'm going wrong.

    Thanks again in advance,

    ~Shad

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,360
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: FileNotFoundException!?

    If I could figure out which directory the program checks
    Salt all the possible directories with the same named file but with a different contents.
    Run the program and look at the contents of the file that is read. That will tell you which file was read from where.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: FileNotFoundException!?

    To specify the full path you must have the /home/etc... (eg '/home/cassandra/workspace/FumeTracker/myfilename') To specify a relative path, do not include the first backslash. You can backtrack to a parent directory using the ''../', to specify folders, use the '/'. For instance, if I wish to read a file in a directory found in the parent directory:
    '../dir/myfile.txt'. Relative paths should be relative to the calling class...if the class is in a package, then its relative to the package.Try something like '../src/herbmasterlist.txt'
    Last edited by copeg; June 29th, 2010 at 06:50 PM.