(2019) Example File IO and Exception Handling
(2019) Example File IO and Exception Handling
import java.util.*;
import java.io.*;
import java.util.*;
import java.io.*;
//using try-catch: give meaningful message for each exception type so
//that it would be easy to understand the error
public class TryCatch
{
public static void main(String [] args)
{
try
{
FileReader inFile = new FileReader("result.txt");
BufferedReader inReader = new BufferedReader(inFile);
import java.util.*;
import java.io.*;
public class TestProgram
{
public static void main(String [] args)
{
try
{
Program progDetail [] = new Program[12];
FileReader inFile = new FileReader("programDetail.txt");
BufferedReader inReader = new BufferedReader(inFile);
pw.println("Program Code\tDuration\tFaculty
\tProgram Head\tLevel");
int i=0;
while((inData = inReader.readLine()) != null)
{
StringTokenizer input = new StringTokenizer(inData,",");
code = input.nextToken();
duration = Integer.parseInt(input.nextToken());
faculty = input.nextToken();
head = input.nextToken();
if(progDetail[x].programLevel().equalsIgnoreCase("Diploma"))
pw.println(progDetail[x].toString() + "\t" + progDetail[x].programLevel());
if(progDetail[x].programLevel().equalsIgnoreCase("Certificate"))
pw.println(progDetail[x].toString() + "\t" + progDetail[x].programLevel());
}
inReader.close();
pw.close();
}//end of try block
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}
catch(IOException io)
{
System.out.println(io.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Sample Input:
Sample Output: