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

Thread: Collecting Data from File

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Collecting Data from File

    Hi guys,

    So I have a problem getting some Data from a particular file
    In this context I used API I/O and this is my chunk of code

    Scanner scanner = new Scanner(System.in);
    			System.out.print("Enter filename: ");
    			String file;
     
    			file = scanner.next();
    			//checks if the filename by the user exists
    			try{
    				dataFile = new Scanner(new File(file));
    			}
    			catch (FileNotFoundException e){
    				e.printStackTrace();
    			}  
    			scanner.close();

    The file is located right outside the package of all the code and program
    It always return an exception of the file not found and printing the stacktrace


    Thanks beforehand if anyone could help out


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Collecting Data from File

    Are you giving relative path of the file to be read?.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    Quote Originally Posted by dineshj83 View Post
    Are you giving relative path of the file to be read?.
    No,

    I want the program to work not only in my pc, and that's why I cannot use a specific path that only works in my Pc
    but the path itself is always right outside the package.

  4. #4
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Collecting Data from File

    Thats why i asked you to relative path which will work even if you package it and share it as jar to external world. Only in absolute path you will point to any of your drives in PC. Hope you got it now.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    I think relative path is used to specify the path has to be there, or can we give the path to link it for the directory outside the package?

    For example, if we put my package in the desktop, and the data also in the desktop
    how do you make a relative path that the data location is outside the package?

    Note: Sorry I'm still a newbie

    --- Update ---

    OH nevermind,
    I get it now,
    But could you give a simple example to create a relative path which in here i want it located outside my package?
    I've never using this kind of method so it probably would help me a lot to implement this way.

  6. #6
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Collecting Data from File

    For example if your code is in src/<package>/file and your file is in the same level as src then give only file name if its in a folder then it will be like foldername/filename. Hope you got it now.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    I know the concept now,
    but still confused how to implement it
    Could you / anyone explain how to implement the relative path inside my code?
    I've been trying to write it like this
    Scanner scanner = new Scanner(System.in);
    			System.out.print("Enter filename: ");
     
    			File file = new File(scanner.next());
     
     
    			// checks if the filename by the user exists
     
    			try {
     
    				dataFile = new Scanner(file);
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			}
     
    			scanner.close();
     
    		}
    so what's next?

  8. #8
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Collecting Data from File

    Your input on the console will be the filename and hence that has to be fed as relative path as explained.

  9. The Following User Says Thank You to dineshj83 For This Useful Post:

    blacksteel (November 29th, 2013)

  10. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    Quote Originally Posted by dineshj83 View Post
    Your input on the console will be the filename and hence that has to be fed as relative path as explained.
    What i was asking was, what do i need to write/add inside my code to make things(relative path) work

    Sorry for asking too much, because this is my first time using this kind of way

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Collecting Data from File

    Can you give examples of the file structure for the package and the location of the file? I'm not sure what you mean by the file being "right outside the package." Post code that demonstrates the problem, if possible.

  12. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    Quote Originally Posted by GregBrannon View Post
    Can you give examples of the file structure for the package and the location of the file? I'm not sure what you mean by the file being "right outside the package." Post code that demonstrates the problem, if possible.
    a.jpg
    I need to collect the data inside "DataFile"
    My program starts inside the PJ3.package(GasStationSimulator.java)

    My current code is the same as the first post

    What i want to happen, when I type the file name ( which is "DataFile" ) I can retrieve the data and use it for other method
    But with my current code, it always returning an exception of the file not found

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Collecting Data from File

    For me, the statement that would work in your case is:

    File = new File( "./src/PJ3/DataFile" );

    (notice the leading '.' or period.)

    So you'll need to "condition" the user's input with the appropriate relative file location and then use the resulting string instead of the user's input directly.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    blacksteel (November 29th, 2013)

  15. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Collecting Data from File

    Actually i figured out a very small mistake by accident
    Thanks to both of your help for the assistance

    Note for other people references : In fact, my code was working perfectly fine. If I compile and run in through cmd it works perfectly fine and read the file i wanted to open.
    Since i tried to run it using IDE (Eclipse) which create some default directory and few supporting files. It mislead the targeting directory that i want.(by adding another folder with the name "src")

    So in general for other people who face the same problem like I did, my tips will be, check your original directory by printing the current directory
    System.out.println(System.getProperty("user.dir");

Similar Threads

  1. Replies: 1
    Last Post: October 30th, 2013, 09:55 PM
  2. Collecting user input error
    By SkyVar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 19th, 2013, 09:48 PM
  3. Binary File. Extract float data from binary file and put into array
    By cardinal152 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 13th, 2012, 07:31 PM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. collecting information/moving from servlet to servlet
    By CBird in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 07:04 PM