Results 1 to 4 of 4
  1. #1
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default Help reading file


    I'm having trouble getting one part of my program to work. I want this part of the program to load a file from the scanner input from a specific pathway when i type the file in instead of having to enter the whole pathway. I'm pretty sure its the correct pathway but it's not loading. Any tips? I'm not really sure whats wrong.

    Java Code:
    public static Scanner getinput()
    	String filein = JOptionPane.showInputDialog(null,"Enter Data file name","Input",JOptionPane.QUESTION_MESSAGE);		
    	File f = null;
    	if (f == null)
    		f = new File("Users\\brian\\Documents\\workspace\\blahblah\\src\\" + filein);
    	Scanner reader = null;
    		reader = new Scanner (f);
    	catch (FileNotFoundException e)
    			System.out.println("File not found. Please try again.");
    	return reader;

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Help reading file

    If that's a Windows path, it should start with a drive letter. If it's a Linux path, it should use forward slashes (/) not backslashes (\).

    The forward slashes also work for Windows, and don't require escaping.

    And it's meaningless to do a null test for f one line after you've declared it as null.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: Help reading file

    What error do you get?
    and this path is wrong: "Users\\brian\\Documents\\workspace\\blahblah\\src \\"

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Help reading file

    Or is Users a directory in the current directory when the program is executing?

    To see where the computer is looking for the file, print the file object's absolute path. See API doc for the correct spelling of the method to get the path.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Reading from a file.
    By Quizzle23 in forum New To Java
    Replies: 5
    Last Post: 03-17-2011, 01:28 PM
  2. Reading file external to jar file
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 06:46 AM
  3. reading a file and writing to a!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 07:53 PM
  4. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  5. Replies: 3
    Last Post: 05-10-2009, 12:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts