Presentation on File Handling

Download Report

Transcript Presentation on File Handling

2. Program Construction
in Java
1
2.11 File Handling (SL)
File handling
• Most of your programming work will involve
the use of databases.
• These require their records to be loaded and
saved on a regular basis to and from the hard
drive.
3
Data streams
• You need to create an object called a data
stream to handle the operations (look on this
as the pipe through which the data flow).
• The following code can be in a Class of its
own...
4
‣
Class LoadData
// import a java io toolkit
import java.io.*;
// start the class
public class LoadData {
//which has one method
public static void loadFile ()
{
// create the data stream
here
}
5
Class LoadData
‣
import java.io.*;
public class LoadData {
public static void loadFile ()
{
...
}
6
‣
Class LoadData
import java.io.*;
public class LoadData {
public static void loadFile ()
{
// create the data stream
BufferedReader dataIn;
dataIn = new
BufferedReader(new
FileReader("datafile.dat"));
}
}
7
Class LoadData
• However, there are numberous dangers in
connecting the data stream, so here you must
make use of a try...catch block:
8
‣
import java.io.*;
Class
LoadData
public
class
LoadData {
public static void loadFile ()
{
BufferedReader dataIn;
try {
dataIn = new
BufferedReader(new
FileReader("datafile.dat"));
}
catch (FileNotFoundException
e) {
IBIO.output("Can't find
file!");
}
9
Class LoadData
• Now that the data stream is connected, read
in the data one line at a time and allocate it to
a variable...
10
Class LoadData
‣
for (int i = 0; i<=9 ; i++) {
Reg.names [i] = dataIn.readLine();
Reg.forms[i] = dataIn.readLine();
//etc.
}
11
Class LoadData
• More possible problems!
‣
again try...catch blocks are required
12
Class LoadData
‣
try {
for (int i = 0; i<=9 ; i++) {
Reg.names [i] = dataIn.readLine();
// etc.
}
}
catch (IOException e) {
IBIO.output("Input Error: " +
e.getMessage());
}
catch (IndexOutOfBoundsException e)
{} //etc.
}
13
Class LoadData
• Notes:
‣
‣
You need to import a Java toolkit from the
ready-made Java library right at the start.
The line dataIn = new
BufferedReader(new
FileReader("datafile.dat"));
creates the data stream (match the name of
the data file to that in your project).
14
Class LoadData
‣
‣
The data stream reads one line at a time
and presents it as a String (this may need
conversion to int or boolean!).
The try...catch blocks attempt to catch
any exceptions that may be thrown e.g. file
not found.
15
Class LoadData
• At the end of the process, the data
has to be closed:
‣
finally {
dataIn.close();
}
16
stream
Class LoadData
• Strings get loaded with a simple
= dataIn.readLine();
• For ints use
= Integer.parseInt(dataIn.readLine());
• For booleans use
= Boolean.getBoolean(dataIn.readLine());
• For chars use
= dataIn.readLine().charAt(0);
17
Class SaveData
• Open the Class SaveData and look at its
code.
• It uses similar tools to save data back to the
hard drive (note the comments).
18
Disadvantage
• The data are accessed sequentially i.e. one
after the other on the hard drive - there is no
way to go directly to just the record you want.
• For this reason, you must load or save the
whole data file.
19
Footnotes
• At HL, you use a similar technique allowing
direct (= random) access to the data.
• Mastering file I/O techniques saves so much
time when testing your program.
20