Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming

2 SE15: File I/O Part 231–2 Today’s Learning Objectives To understand the File Class To Learn how to use the classes ObjectOutputStream and ObjectInoutStream to write and read binary files

3 SE15: File I/O Part 231–3 Lecture Outline The File Class Binary file I/O Object I/O with object Streams Using the Serializable interface Savitch Chapter 9

4 SE15: File I/O Part 231–4 Using the File Class The File class is a wrapper class for file names. The constructor takes a String as an argument and produces an object that can be thought of as a file with that name. It also allows you to test properties of the file such as if a file exists or if you have permission to read it. File fileObject = new File(“myfile.txt”); if(! fileObject.exists()) System.out.println(“No file by that name”); if(! fileObject.canRead()) System.out.println(“Not allowed to read that file”);

5 SE15: File I/O Part 231–5 Some Methods of the File Class exists() boolean canRead() boolean canWrite() boolean delete() tries to delete the file long length() String getName() Returns the name of the file (not path) String getPath() Returns the path name

6 SE15: File I/O Part 231–6 Binary File I/O Files are stored in the same form as they are in the computers memory. This means they are efficient Binary files created by a Java program can be moved between computers and still read by a Java program – but only a Java program. The preferred classes for processing binary files are: ObjectInputStream ObjectOutputStream

7 SE15: File I/O Part 231–7 Opening a binary file for writing Opening a binary file for writing is similar to text files The ObjectOutputStream class has no constructor that takes a file name as its argument so we use the class FileOutputStream to create a stream that can be used as an argument to the ObjectOutputStream constructor e.g. new FileOutputStream(“out.dat”) ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(“out.dat”));

8 SE15: File I/O Part 231–8 Writing primitive types to binary files Once a stream class is connected to a file, you can write various primitive types to a file using the methods of ObjectOutputStream. e.g. outputStream.writeInt(n); which writes the variable integer variable n a file. There are similar methods: writeDouble(double n) writeFloat(float n) writeChar(int n) writeBoolean(boolean b) You can mix up the types you write but you need to know the order in which they appear in the file to be able to read them back again.

9 SE15: File I/O Part 231–9 Writing Strings to binary files writeUTF(String aString) writes the String value of aString to the output stream UTF refers to a particular encoding of the string, you therefore need to use the readUTF method to read it back from the file. outputStream.writeUTF(“Yikes it’s Java”); UTF stands for Unicode Text Format This encodes all Unicode characters, but favours the ASCII character set, giving these short efficient codes.

10 SE15: File I/O Part 231–10 Opening a binary file for reading Again this is similar to text files The ObjectInputStream class has no constructor that takes a file name as its argument so we use the class FileInputStream to create a stream that can be used as an argument to the ObjectInputStream constructor e.g. new FileInputStream(“mydata.dat”) ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(“mydata.dat”)); If your program attempts to read beyond the end of the file a EOFException is thrown.

11 SE15: File I/O Part 231–11 Reading and Writing Objects You can read and write objects to a binary file using the: writeObject method of the ObjectOuputStream class and the readObject method of the ObjectInputStream class. This requires the class to Serializable by adding implements Serializable to the heading of the class definition public class Library implements Serializable { This also requires import java.io.*

12 SE15: File I/O Part 231–12 The Serializable Interface What is the effect of making a class serializable? Java assigns a serial number to each object it writes to a stream. If the object is written more than once then after the first time Java only writes the serial number This makes I/O more efficient and files smaller If a Serializable class has instance variables of a class type then the class for the instance variables must also be Serializable and so on for all levels of instance variables within classess.

13 SE15: File I/O Part 231–13 Summary Looked at the File Class Looked at examples of reading and writing binary files containing Primitive types Strings Objects using the Serializable interface

14 SE15: File I/O Part 231–14 Follow-up Work Savitch chapter 9 Exercise 7


Download ppt "Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google