Chapter 7 - File Input OutputVer2
Chapter 7 - File Input OutputVer2
File Input/Output
By
En. Mohd Nizam bin Osman
(Senior Lecturer)
Department of Computer Science
Faculty of Computer and Mathematical Science, UiTM
Perlis
Objectives
• By the end of this chapter, students should be able to:
Advantages
Character
the smallest
Field
data, any one
of letters, grouped of
Record
numbers, related
character; field are
File
special
symbols on the meaningful grouped
together. related records
Database
keyboard or data such as
hidden name, age. are grouped
together. consists of
computer several related
characters. files.
6
Introduction
(The Concept of a Stream)
• In java, file I/O, as well as simple keyboard and screen I/O, is
handled by stream.
File
Text Binary
Text files
files file
9
Text Files
• All of the data in any file is stored as binary digits
(bits) – sequence of 0s and 1s.
12
Reading and Writing Data
(Basic Operations for Writing to a File)
• Six basic operations:
Writing to a File
Create and save an empty data file using non-document
text editor. (Optional)
Create a class (an application class) as a medium of writing
data to the data file.
In the class, create a variable to represent the data file in
the program.
Then, create an output stream and attach it to the file
variable.
Exception
Checked Unchecked
18
Text – File Input/Output
• We can solve the problem by using three different
approaches:
Approaches
(File I/O)
19
Reading and Writing - File
(Example)
21
Reading and Writing - File
(Example (Example 1 – Using main
method only)
STEP 3: Read data from
String inData = null;
while((inData = in.readLine()) != null) input
file (as record)
{
StringTokenizer st = new StringTokenizer (inData,";");
String name = st.nextToken();
STEP 4: Tokenize
double time1 = Double.parseDouble(st.nextToken()); the record into
double time2 = Double.parseDouble(st.nextToken(););
double time3 = Double.parseDouble(st.nextToken(););
field/attribute
double average = (time1 + time2 + time3)/3;
}//end while
in.close();
outPass.close(); STEP 6: Close ALL files
outFail.close();
}//end try
STEP 7: End block try
22
Reading and Writing - File
(Example (Example 1 – Using main
method only))
catch(FileNotFoundException fe)
{
System.out.println(fe.getMessage());
}
STEP 8:
catch(IOException iox) Exception
{ Handling
System.out.println(iox.getMessage()); (Catch Block)
}
catch(Exception e)
{
System.out.println("problem: " +e.getMessage());
}
}//end main
}//end class
23
Reading and Writing - File
(Example 2 – Using an Array)
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
24
Reading and Writing - File
(Example 2 – Using an Array)
STEP 2: Exception
try{
BufferedReader in = new BufferedReader (new Handling (Block
Try)
STEP 3: FileReader("D:\\runner.txt"));
Open PrintWriter outPass = new PrintWriter (new BufferedWriter(new
ALL file FileWriter("D:\\pass.txt")));
PrintWriter outFail = new PrintWriter (new BufferedWriter(new
(Input +
FileWriter("D:\\fail.txt")));
Output) STEP 4: Read data from
String inData = null; input file (as record)
while((inData = in.readLine()) != null)
{
StringTokenizer st = new StringTokenizer (inData,";"); STEP 5:
name[cnt] = st.nextToken(); Tokenize
time1[cnt] = Double.parseDouble(st.nextToken()); the record
time2[cnt] = Double.parseDouble(st.nextToken());
time3[cnt] = Double.parseDouble(st.nextToken()); into field/
cnt++; attribute
}//end while
STEP 6: Count
records
25
Reading and Writing - File
(Example 2 – Using an Array)
for(int i=0; i<cnt; i++)
{
double average = (time1[i] + time2[i] + time3[i])/3;
}//end for
in.close();
outPass.close(); STEP 8: Close ALL files
outFail.close();
}//end try
STEP 9: End block try
26
Reading and Writing - File
(Example 2 – Using an Array)
catch(FileNotFoundException fe)
{
System.out.println(fe.getMessage());
}
STEP 10:
catch(IOException iox) Exception
{ Handling
System.out.println(iox.getMessage()); (Catch Block)
}
catch(Exception e)
{
System.out.println("problem: " +e.getMessage());
}
}//end main
}//end class
27
Reading and Writing - File
(Example 3 – Using OOP)
import java.util.*;
import java.io.*;
//Default constructor
public Runner()
{
name = "";
time1 = 0;
time2 = 0;
time3 = 0;
}
28
Reading and Writing - File
(Example 3 – Using OOP)
//Normal constructor
public Runner(String nm, double tm1, double tm2, double tm3)
{
name = nm;
time1 = tm1;
time2 = tm2;
time3 = tm3;
}
//Setter
public void setName(String nm)
{
name = nm;
}
//Processor
public double calAverage()
{
return ((time1+time2+time3)/3.0);
}
31
Reading and Writing - File
(Example 3 – Using OOP)
//Printer
public String toString()
{
return "\nName: "+name + "\nTime 1:"+Time1+"\nTime 2:“
+time2+"\nTime 3: "+time3;
}
32
Reading and Writing - File
(Example 3 – Using OOP)
import java.util.*;
import java.io.*;
try{
BufferedReader in = new BufferedReader (new
FileReader("E:\\runner.txt"));
STEP 2: PrintWriter outPass = new PrintWriter (new
Open ALL BufferedWriter(new FileWriter("E:\\pass.txt")));
file (Input + PrintWriter outFail = new PrintWriter (new
Output) BufferedWriter(new FileWriter("E:\\fail.txt")));
33
Reading and Writing - File
(Example 3 – Using OOP)
STEP 3: Declare array
//Declare an array of object of object
Runner[] run = new Runner[100];
in.close();
outPass.close(); STEP 9: Close ALL files
outFail.close();
}//end try
STEP 10: End block try
35
Reading and Writing - File
(Example 3 – Using OOP)
catch(FileNotFoundException fe)
{
System.out.println(fe.getMessage());
}
STEP 11:
catch(IOException iox) Exception
{ Handling
System.out.println(iox.getMessage()); (Catch Block)
}
catch(Exception e)
{
System.out.println("problem: " +e.getMessage());
}
}//end main
} //end class
36
The End
Q&A
37