Results 1 to 4 of 4
Thread: Help reading file
- 03-31-2012, 05:07 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 8
- Rep Power
- 0
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; }
- 03-31-2012, 05:14 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-31-2012, 05:16 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
Re: Help reading file
What error do you get?
and this path is wrong: "Users\\brian\\Documents\\workspace\\blahblah\\src \\"
- 03-31-2012, 05:30 PM #4
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
-
Reading from a file.
By Quizzle23 in forum New To JavaReplies: 5Last Post: 03-17-2011, 12:28 PM -
Reading file external to jar file
By nn12 in forum New To JavaReplies: 6Last Post: 02-04-2011, 05:46 AM -
reading a file and writing to a file....help!!!!
By java_prgr in forum New To JavaReplies: 3Last Post: 07-26-2010, 06:53 PM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks