Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file reader

Status
Not open for further replies.

shotcalla

Technical User
Oct 25, 2009
2
0
0
CA
Hi,

I am trying extract data into a file and then put it into a multidim array.

I when execute the code Im getting a complie error with a problem with my fileReader class. The error i get is :

Code:
Unhandled exception type FileNotFoundException

This is my code

Code:
	/* TODO import data from .csv file   */
		
		String[][] serverData = new String[3][16];
		 
		final String FILE_NAME = "C:\\testFile.csv";
		BufferedReader inputFromFile = null;
		inputFromFile = new BufferedReader(new FileReader(FILE_NAME));	//This is where the problem occurs	
		String line = null;
		int row = 0;
		int col = 0;
		
		//read each line of text file using StringTokenizer
		while((line = inputFromFile.readLine()) != null)
		{
			StringTokenizer StringTok = new StringTokenizer(line,",");
			while (StringTok.hasMoreTokens())
			{
				//store line in array
				serverData[row][col] = StringTok.nextToken();
				col++;
			}
			row++;
		}
		 
		inputFromFile.close();

This is the line where the problem occurs:

Code:
inputFromFile = new BufferedReader(new FileReader(FILE_NAME));	//This is where the problem occurs

I know that I have inputted the file name and location correctly. i have also imported all of the necessary classes. I am not sure what the problem is though!?
 
Look closely at the constructor for FileReader in the API docs. You would see that it throws a FileNotFoundException which is an uncaught exception. Uncaught exceptions are exceptions that are not implicitly handled by the JVM and you code has to ensure that they are caught and handled properly. This is exactly what the compiler is asking you to do.

So, just wrap your code in a try/catch block like so:

Code:
try 
{
inputFromFile = new BufferedReader(new FileReader(FILE_NAME));    //This is where the problem occurs    
        String line = null;
        int row = 0;
        int col = 0;
        
        //read each line of text file using StringTokenizer
        while((line = inputFromFile.readLine()) != null)
        {
            StringTokenizer StringTok = new StringTokenizer(line,",");
            while (StringTok.hasMoreTokens())
            {
                //store line in array
                serverData[row][col] = StringTok.nextToken();
                col++;
            }
            row++;
        }
        
        inputFromFile.close();
}
catch(FileNotFoundException notFound)
{
   //Do something here
}
catch(Exception e)
{
   //Do something here
}

Hope this helps.

Nitin
 
Some points:

- I'd not use a try/catch for all Exceptions, that may mask some unrelated exceptions that will be caught without reason, hiding the error
- I'd not use a single try/catch block, you may want to do different actions if the exception occurs opening, reading or closing the file

Appart from that:

- Why declaring a final variable inside a method?
- StringTokenizer is deprecated, use String.split instead
- I don't like capitalized variable names.

Cheers,
Dian
 
I agree with Dian. Also, why does your example contain so much unnecessary code?

Instead of:

Code:
final String FILE_NAME = "C:\\testFile.csv";
BufferedReader inputFromFile = null;
inputFromFile = new BufferedReader(new  FileReader(FILE_NAME));

why not use this?

Code:
BufferedReader inputFromFile = new BufferedReader(new FileReader("C:\\testFile.csv"));

Cheers, Tom
 
Using backslashes sometimes doesn't work. Try "C://testFile.csv".
 
The forward slash is not a standard escape character and does not need escaping, so the alternative pathname should be written like "C:/testFile.csv".
Btw "C:\\testFile.csv" should be working just as fine with a standard Java runtime on Windows.

HTH
 
You're right TonHu, I misread and messed up a bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top