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

    Default Help reading file

    Hey,

    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;
    
    	try
    	{
    		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
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    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.

    db
    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
    Posts
    251
    Rep Power
    4

    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 online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    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 file....help!!!!
    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
  •