0% found this document useful (0 votes)
26 views

(2019) Example File IO and Exception Handling

The document discusses file input/output and exception handling in Java using examples. It shows how to handle exceptions using the throws keyword and try-catch blocks. It also demonstrates file I/O and object-oriented programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

(2019) Example File IO and Exception Handling

The document discusses file input/output and exception handling in Java using examples. It shows how to handle exceptions using the throws keyword and try-catch blocks. It also demonstrates file I/O and object-oriented programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

File IO and Exception Handling

Example 1: Exception Handling Using Throws Keyword

import java.util.*;
import java.io.*;

public class ThrowsKeyword


{
public static void main(String [] args) throws IOException
{
FileReader inFile = new FileReader("result.txt");
BufferedReader inReader = new BufferedReader(inFile);

FileWriter fw = new FileWriter("AD.txt");


BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

String inData = null;


String name, gender;
double cgpa;
while((inData = inReader.readLine()) != null)
{
StringTokenizer input = new StringTokenizer(inData,"#");
name = input.nextToken();
gender = input.nextToken();
cgpa = Double.parseDouble(input.nextToken());

if(cgpa >= 3.50)


pw.println(name + "\t" + gender + "\t" + cgpa);
}//end of while
inReader.close();
pw.close();
}
}
Example 2: Exception Handling Using try-catch block

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

FileWriter fw = new FileWriter("AD.txt");


BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

String inData = null;


String name, gender;
double cgpa;
while((inData = inReader.readLine()) != null)
{
StringTokenizer input = new
StringTokenizer(inData,"#");
name = input.nextToken();
gender = input.nextToken();
cgpa = Double.parseDouble(input.nextToken());

if(cgpa >= 3.50)


pw.println(name + "\t" + gender + "\t" + cgpa);
}//end of while
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());
}
}
}
Example 3: File IO and OOP

public class Program{


private String progCode;
private int progDuration;
private String faculty;
private String progHead;

public Program(String progCode, int progDuration, String faculty, String progHead)


{
this.progCode = progCode;
this.progDuration = progDuration;
this.faculty = faculty;
this.progHead = progHead;
}

public String getProgCode(){return progCode;}


public int getProgDuration(){return progDuration;}
public String getFaculty(){return faculty;}
public String getProgHead(){return progHead;}

public String programLevel()


{
String progL = null;
char level = progCode.charAt(2);
if(level == '0')
progL = "Certificate";
else if(level == '1')
progL = "Diploma";
else if(level == '2')
progL = "Degree";
else if(level == '7')
progL = "Master";
else if(level == '9')
progL = "Doctorate";
return progL;
}
public String toString()
{
return progCode + "\t\t" + progDuration +
"\t\t" + faculty + "\t\t" + progHead;
}
}

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

FileWriter fw = new FileWriter("details.txt");


BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

pw.println("Program Code\tDuration\tFaculty
\tProgram Head\tLevel");

String inData = null;


String code, faculty, head;
int duration;

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();

progDetail[i] = new Program(code,duration,faculty,head);


i++;
}

for(int x=0; x<progDetail.length; x++)


{

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:

You might also like