Results 1 to 3 of 3
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation 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
    Java 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");
    		}
    in the seatFile contains
    Java Code:
    4
    Pestilence 4342
    Famine 3932
    War 2975
    Death 4837
    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 txt
    im using eclipse to code the project in
    any help would be much appreciated.
    and thank you in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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).

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

Similar Threads

  1. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 10:07 AM
  2. Reading files and splitting them
    By Dieter in forum Advanced Java
    Replies: 5
    Last Post: 09-30-2009, 10:46 AM
  3. Reading growing files
    By ionna22 in forum Advanced Java
    Replies: 0
    Last Post: 10-20-2008, 05:43 PM

Posting Permissions

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