Files, Streams, and Serialization
Objective
- Read and write text files.
- Work with object serialization.
Summary
- A stream represents ordered data flowing to or from a file. Java views each file as a sequential stream of bytes.
- Character-based streams store data as text characters (text files), while byte-based streams store data in binary format (binary files).
- Common classes for text file I/O include
Formatter,FileWriter,Scanner,FileReader, andPrintWriter. - Object serialization allows entire objects to be written to or read from files as a sequence of bytes containing both data and type information.
- Classes must implement the
Serializableinterface to enable serialization and deserialization usingObjectOutputStreamandObjectInputStream.
Exercise 1*
Text Files: Working with Formatter and File
-
Create a new project and add the following class to it. Run the application and type any text that you would like to save. When you finish typing, press
<Ctrl+Z>or<Ctrl+D>to end the input, then open the fileoutput.txtand check its content.FormatterDemo.javaimport java.io.File; import java.io.IOException; import java.util.Formatter; import java.util.Scanner; public class FormatterDemo { public static void main(String[] args) { Formatter output = null; Scanner scanner = new Scanner(System.in); try { // output = new Formatter("output.txt"); output = new Formatter(new File("output.txt")); } catch (IOException ioe) { System.out.println("File could not be opened/created. An exception occurred.\n" + ioe); } if (output != null) { System.out.println( "Enter the data to be saved. When done, press <Ctrl+Z>/<Ctrl+D>:"); while (scanner.hasNext()) { String line = scanner.nextLine(); output.format("%s%n", line); } output.close(); } scanner.close(); } } -
Run the program and enter your name and then finish the input, then open the file and see what you typed. Close the file, run the program once more and then type
CMPS 251 Laband then terminate your program. Does it replace the previous content or is it added at the end of the file? -
How can you add new content to an existing file (append) without replacing the old content?
Exercise 2*
Text Files: Working with Formatter and FileWriter
-
Add the following class to your project. Run the application and type any text that you would like to save. When you finish typing, press
<Ctrl+Z>or<Ctrl+D>to end the input, then open the fileoutput.txtand check its content.FileWriterDemo.javaimport java.io.FileWriter; import java.io.IOException; import java.util.Formatter; import java.util.Scanner; public class FileWriterDemo { public static void main(String[] args) { FileWriter writer = null; Formatter output = null; Scanner scanner = new Scanner(System.in); try { writer = new FileWriter("output.txt", true); output = new Formatter(writer); } catch (IOException ioe) { System.out.println("File could not be opened/created. An exception occurred.\n" + ioe); } if (output != null) { System.out.println( "Enter data to be saved. When done, press <Ctrl+Z>/<Ctrl+D>:"); while (scanner.hasNext()) { String line = scanner.nextLine(); output.format("%s%n", line); } output.close(); } scanner.close(); } } -
Run the program once more and enter your name and then finish the input. Open the file and see what you typed. Does it replace the previous content, or it is added at the end of the file?
-
What is the purpose of the
truekeyword when provided as a second argument in theFileWriterconstructor? -
What happens to the file content if you do not provide this second argument?
Exercise 3*
Object Serialization: Working with ObjectOutputStream and ObjectInputStream
-
Add the following two classes to your project then run the app and examine the output:
ObjectSerialization.javaimport java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectSerialization { public static void main(String[] args) { try { ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("persons.dat")); Person person1 = new Person("John"); Person person2 = new Person("Jane"); // Saving objects output.writeObject(person1); output.writeObject(person2); output.close(); // Reading objects ObjectInputStream input = new ObjectInputStream(new FileInputStream("persons.dat")); Object object; Person person; while ((object = input.readObject()) != null) { person = (Person)object; System.out.println(person.getName()); } input.close(); } catch (EOFException eof) { System.out.println("Reached end of file."); } catch (IOException | ClassNotFoundException exception) { System.out.println(exception.getStackTrace()); } } }Person.javapublic class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } -
Did you obtain the names of the two created
Personobjects? Were they saved? If not, why? -
Let the
Personclass implement theSerializableinterface and then run the application again. Compare the output with that of the previous run. What is your conclusion?
Exercise 4
The names.txt file contains a list of names along with their gender and origin:
Name Gender Origin
Khalid M Arabic
Alia F Hindi
Fatima F Arabic
Sara F Hebrew
Omar M Arabic
Eman F Arabic
Farah F Hebrew
Ali M Arabic
Ahmed M Arabic
Omnia F Arabic- Write an application that opens the file and lists the female names of Arabic origin. Your listing should only show the name and gender; do not show the origin as it is that same for all the filtered output.
- Properly format your output using a tabular layout.
- Send a copy of the output to a text file called
arabic-female-names.txt.
Exercise 5
-
Extend the
Personclass of Exercise 3 into anEmployeeby adding two more fields:id:intandsalary:double. Add setters/getters and define a constructor to have three parameters to initialize all fields. -
Write a new application that asks the user to enter an employee’s information, create a
Employeeinstance, and then save it in a file of objects namedemployees.dat. The program continues to accept data and sends them as objects to the above file. The program terminates when the user enters a negative id. -
Write another application that opens the
employees.datfile and loads its contents in anArrayListof typeEmployee. -
Write a method called
getEmployee(int searchId)that searches the above list for an employee withidequals to thesearchId. If found the function returns thatEmployeeinstance, and, if not found the method returnsnull. -
Test the
getEmployee()method by printing the salary for the search employee, if found.
Exercise 6+
-
Write a program to read students’ data that includes their name:
Stringand three grades:double. Your program should ask the user at the beginning to input how many students they would like to enter, then keep reading the records while saving them to a file calledstudents.txt. -
Write a program to read students’ data from the file saved previously, and display each students’ name and average on a separate line.
Exercise 7+
-
Create a text file,
records.txt, that contains data about employees as follows:ID Name Salary 234 Sara 4500.00 987 Khaled 7345.12 997 Nadim 1267.00 523 Noora 8230.00 -
Write a program that reads data from the file
records.txtto anArrayListofEmployee, then save thisArrayListto a binary fine calledrecords.dat, as a single object. Reuse theEmployeeclass from Exercise 5. -
Write another program to retrieve the data saved in the file
records.dat, and display the employee records on the screen.