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 6 of 6

Thread: I can't get Relative paths to work with the code I'm using.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I can't get Relative paths to work with the code I'm using.

    Hello, I have been working on this all day. I can't get Relative paths to work. I have created the class.dat file and I can't get java to recognize it. I am using Eclipse as an IDE. Was wondering if I could get Eclipse to recognize it. I tried with a .txt file as well and couldn't get that to work.



    import java.io.*;
     
    public class ReadBytes
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			FileInputStream file = new FileInputStream("class.dat");
    			boolean eof = false;
    			int count = 0;
    			while(!eof)
    			{
    				int input = file.read();
    				System.out.print(input + " ");
     
    				if (input == -1)
    					eof = true;
    				else
    					count++;
    			}
    			file.close();
    			System.out.println("\nBytes read: " + count);
     
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error -- " + e.toString());
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't get Relative paths to work with the code I'm using.

    Create a File object using the filename and print its absolute path to see where the program is looking for the file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't get Relative paths to work with the code I'm using.

    Quote Originally Posted by Norm View Post
    Create a File object using the filenname and print its absolute path to see where the program is looking for the file.
    I'm sorry, just a beginner, not sure exactly how to do that. How do I create a File object and then print its absolute path. I found the code from an old textbook call Java 2 in 21 days.

    Would appreciate if you could break it down for me.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't get Relative paths to work with the code I'm using.

    Something like this:
    File aFile = new File<THEPathHERE>); // create a File object
    System.out.println("an ID "+ aFile.getabolutepath()); // print its absolute path

    check the spelling.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't get Relative paths to work with the code I'm using.

    Quote Originally Posted by Norm View Post
    Something like this:
    File aFile = new File<THEPathHERE>); // create a File object
    System.out.println("an ID "+ aFile.getabolutepath()); // print its absolute path

    check the spelling.
    Hi Norm

    Here is my code:
    import java.io.*;
     
    public class ReadBytes
    {
    	public static void main(String[] args)
    	{
     
    		File afile = new File("C:/Users/scott/Desktop/Midterm Quiz/Midtermexam");
    		System.out.println("an ID "+ afile.getAbsolutePath());
     
     
     
    	}
    }

    I get the absolute path in the console back as C:\Users\scott\Desktop\Midterm Quiz\Midtermexam

    How do I get the relative path?

    Thanks

    S

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't get Relative paths to work with the code I'm using.

    The code in post#1 was having the problem. You need to find out where the program was looking for the file referenced there:
    FileInputStream file = new FileInputStream("class.dat");

    You want to know where the program is looking for the file: class.dat.
    From post#2:
    Create a File object using the filename: class.dat and print its absolute path to see where the program is looking for the file.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NetBeans IDE GlassFish Environment Relative Paths. Or is it?
    By rjpool@btinternet.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 25th, 2013, 02:31 PM
  2. absolute and relative path
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 20th, 2013, 10:34 AM
  3. [SOLVED] All the paths between 2 nodes in a graph
    By Alhazred in forum Algorithms & Recursion
    Replies: 6
    Last Post: May 15th, 2012, 07:49 AM
  4. Need help with Java's Paths class
    By mshock79 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2012, 01:41 PM
  5. Probelm in Path and Paths Classes
    By neo_2010 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 14th, 2009, 10:39 AM