Results 1 to 3 of 3
- 04-26-2010, 12:37 PM #1
Help with reading in a certain types of files
Hello Forum
My program keeps throwing a file not found exception while reading in a file. Now normally i would bother the forum with this but the file was made up by my teacher for my Australian preferential voting program assignment. Hear my code
in the seatFile containsJava Code:try { int seatCount = 0; Scanner seatFileInput = new Scanner(new FileInputStream("seatFile")); /*This next part will read in the no of seats and * no on the roll*/ /*checks if the file has input*/ if(!seatFileInput.hasNext()) { System.out.printf("FATAL ERROR Cannot read seatFile exiting System\n"); System.exit(1); } /*Setting the number of seats*/ setNumOfSeats(Integer.parseInt(seatFileInput.nextLine().trim()); /*In this while loop i have used the class Arrays method fill * to fill the array*/ while(seatFileInput.hasNext()) { for(int i = 0;i < getNumOfSeats();++i) { String seatline = seatFileInput.nextLine(); String [] seatParts = seatline.split(" "); Arrays.fill(this.seats,0,this.seats.length,new Seat(seatParts[0],Integer.parseInt(seatParts[1]))); ++seatCount; } } /*check if the number of seats read in from the file are correct*/ if(seatCount != getNumOfSeats()-1) { System.out.printf("FATAL ERROR insuffient seats read into file\n"); System.exit(1); } seatFileInput.close();/*closing file*/ cndFileReadIn(); } catch(ArrayIndexOutOfBoundsException aioobe) { System.out.printf("Array index out of bound in class Election " + "seatFileReadIn()\n"); } catch(FileNotFoundException fnfe) { System.out.printf("File not found in class Election " + "seatFileReadIn()\n"); System.exit(1); } catch(Exception e) { System.out.printf("Something wrong in class Election " + "seatFileReadIn()\n"); }
should it matter thats its a file that doesnt have a file extension and can java read this file though it not like normal files eg txtJava Code:4 Pestilence 4342 Famine 3932 War 2975 Death 4837
im using eclipse to code the project in
any help would be much appreciated.
and thank you in advance
- 04-26-2010, 01:06 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Of course it can read it. Java, of course, doesn't care what the file extension is, it doesn't even care what the real file type is. You, of course, need to know, and need to program for it correctly, however. But all of that is, seemingly, not your problem. Your problem seems to be not entereing the file correctly. Right before your "new FileInputStream", do System.out.println(System.getPropery("user.dir")) which will print out the programs "current working directory". I can guarantee that it is diferent from where the file is actually located, meaning that the file won't be found in the way you have entered it. The moral of the story? Use complete paths (i.e. C:/bogus/bogus1/bogus.txt) whenever possible, although those full paths will, generally, not be hardcoded, and where not possible, place the file somewhere on the classpath (where you have control of the rest of the path) and use the getResource method(s).
- 04-27-2010, 11:09 AM #3
Similar Threads
-
Reading .txt files
By cvcs1 in forum New To JavaReplies: 3Last Post: 01-20-2010, 09:07 AM -
Reading files and splitting them
By Dieter in forum Advanced JavaReplies: 5Last Post: 09-30-2009, 09:46 AM -
Reading growing files
By ionna22 in forum Advanced JavaReplies: 0Last Post: 10-20-2008, 04:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks