working with files

25
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input and output devices; explain the concept of an I/O stream; describe the basic file-handling techniques used in the Java language; distinguish between text, binary and object encoding of data; distinguish between serial access files and random access files; create and access files in Java using all the above encoding and access methods.

Upload: gerodi

Post on 04-Jan-2016

21 views

Category:

Documents


1 download

DESCRIPTION

Working with files. By the end of this lecture you should be able to:. explain the principles of input and output and identify a number of different input and output devices; explain the concept of an I/O stream ; describe the basic file-handling techniques used in the Java language; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Working with files

Working with files

By the end of this lecture you should be able to:

• explain the principles of input and output and identify a number of different input and output devices;

• explain the concept of an I/O stream;

• describe the basic file-handling techniques used in the Java language;

• distinguish between text, binary and object encoding of data;

• distinguish between serial access files and random access files;

• create and access files in Java using all the above encoding and access methods.

Page 2: Working with files

Input and output

Output: sending information back to the outside world.

Input: receiving information from the outside world

standard input stream

standard output stream

file input stream

file output stream

Page 3: Working with files

Encoding

Java supports three different ways of encoding data:

“Java”“Charatan Kans”“34.99”

TEXTCan be read by text editors as well as programs.

BINARY001011011010000010001001001101001001000

Can only programs.

OBJECT101011101101011010001001011101001 00101

Can only read by another Java program.

Page 4: Working with files

Types of File Access

Serial access

Each item of data is read (or written) in turn.

Random access

We go directly to the record we want .

Page 5: Working with files

The Car Classpublic class Car{ private String registration; private String make; private double price;

public Car (String registrationIn, String makeIn, double priceIn) { registration = registrationIn; make = makeIn; price = priceIn; } public String getRegistration() { return registration; } public String getMake() { return make; } public double getPrice() { return price; }}

Page 6: Working with files

Writing to text files

“XV1 2AS”“Ford”“2499”

Cars.txt

FileWriterPrintWriter

Page 7: Working with files

Writing to text files

private static void writeList (List<Car> carListIn){ try {

FileWriter carFile = new FileWriter ("Cars.txt");PrintWriter carWriter = new PrintWriter (carFile);

for (Car item : carListIn) { carWriter.println ( item.getRegistration() ); carWriter.println ( item.getMake() );

carWriter.println ( item.getPrice() ); } carWriter.close(); } catch (IOException e) { System.out.println("There was a problem writing the file"); }}

Page 8: Working with files

Effects of writing to a text file

Page 9: Working with files

Reading from text files

“XV1 2AS”“Ford”“2499”

Cars.txt

FileReaderBufferedReader

Page 10: Working with files

private static void readList (List<Car> carListIn){ String tempReg; String tempMake; String tempStringPrice; double tempDoublePrice; try {

FileReader carFile = new FileReader ("Cars.txt");BufferedReader carStream = new BufferedReader (carFile);

tempReg = carStream.readLine();while (tempReg != null)

{ // code to read data goes here }

carStream.close(); } // code to catch exceptions goes here}

Page 11: Working with files

tempMake = carStream.readLine();

tempStringPrice = carStream.readLine();

tempDoublePrice =

Double.parseDouble(tempStringPrice);

carListIn.add

(new Car(tempReg,tempMake,tempDoublePrice));

tempReg = carStream.readLine();

Code to read data

Page 12: Working with files

catch(FileNotFoundException e)

{

System.out.println("\nNo file was read");

}

catch(IOException e)

{

System.out.println

("\nThere was a problem reading the file");

}

Code to catch exceptions

Page 13: Working with files

Writing to binary files

001001001100111101010010010001001001111

Cars.bin

FileOutputStreamDataOutputStream

Page 14: Working with files

private static void writeList (List<Car> carListIn){ Car tempCar; try { FileOutputStream carFile = new FileOutputStream("Cars.bin"); DataOutputStream carWriter = new DataOutputStream(carFile); for (Car item : carListIn) { carWriter.writeUTF(item.getRegistration()); carWriter.writeUTF(item.getMake()); carWriter.writeDouble(item.getPrice()); } carWriter.close(); } catch(IOException e) { System.out.println("There was a problem writing the file"); } }

Page 15: Working with files

The effects of writing to a binary file

Page 16: Working with files

Reading from binary files

001001001100111101010010010001001001111

Cars.bin

FileInputStreamDataInputStream

Page 17: Working with files

Reading from a binary file

private static void readList (List<Car> carListIn) { String tempReg; String tempMake; double tempPrice; boolean endOfFile = false; try { FileInputStream carFile = new FileInputStream ("Cars.bin"); DataInputStream carStream = new DataInputStream (carFile); while(endOfFile == false) {

// code to read data here } carStream.close(); } // code to catch FileNotFoundException and IOException here}

Page 18: Working with files

Code to read data

try

{

tempReg = carStream.readUTF();

tempMake = carStream.readUTF();

tempPrice = carStream.readDouble();

carListIn.add(new Car(tempReg, tempMake, tempPrice));

}

catch(EOFException e)

{

endOfFile = true;

}

Page 19: Working with files

Object serialization

Enables us to read and write whole objects from and to files;

The process of converting an object into a stream of data suitable for storage on a disk is called serialization;

Any class whose objects are to be read and written using the above methods must implement the interface Serializable;

import java.io.*;

public class Car implements Serializable

{

// rest of code here

}

Page 20: Working with files

Writing to object files

001001001100111101010010010001001001111

Cars.obf

FileOutputStreamObjectOutputStream

Page 21: Working with files

Writing objects to a file

private static void writeList(List<Car> carListIn){ try { FileOutputStream carFile = new FileOutputStream("Cars.obf"); ObjectOutputStream carStream = new ObjectOutputStream(carFile); for (Car item : carListIn) { carStream.writeObject(item); } carStream.close(); } catch(IOException e) { System.out.println("There was a problem writing the file"); }}

Page 22: Working with files

Reading from object files

001001001100111101010010010001001001111

Cars.obf

FileInputStreamObjectInputStream

Page 23: Working with files

Reading objects from a file

private static void readList(List<Car> carListIn) { boolean endOfFile = false; Car tempCar; try {

FileInputStream carFile = new FileInputStream("Cars.obf"); ObjectInputStream carStream = new ObjectInputStream(carFile);

tempCar = (Car) carStream.readObject();while(endOfFile != true)

{ // code to read data here

} carStream.close(); } // code to catch exceptions here }

Page 24: Working with files

try{

carListIn.add(tempCar);tempCar = (Car) carStream.readObject();

}catch(EOFException e){ endOfFile = true;}

Code to read data

Page 25: Working with files

catch(FileNotFoundException e){ System.out.println("\nNo file was read");}catch(ClassNotFoundException e) // thrown by readObject{ System.out.println ("\nTrying to read an object of an unknown class");}catch(StreamCorruptedException e) // thrown by the constructor{ System.out.println("\nUnreadable file format");}catch(IOException e){ System.out.println("There was a problem reading the file");}