0% found this document useful (0 votes)
2 views

Lecture_09_FileIO

The document provides an overview of file handling and input/output operations in Java, covering concepts such as file streams, text and binary files, and object serialization. It explains how to use Java's NIO classes and interfaces for file manipulation, including reading and writing data to files, and highlights the importance of the Serializable interface for object serialization. Additionally, it includes code examples demonstrating how to read from and write to both text and binary files.

Uploaded by

urbanatuba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture_09_FileIO

The document provides an overview of file handling and input/output operations in Java, covering concepts such as file streams, text and binary files, and object serialization. It explains how to use Java's NIO classes and interfaces for file manipulation, including reading and writing data to files, and highlights the importance of the Serializable interface for object serialization. Additionally, it includes code examples demonstrating how to read from and write to both text and binary files.

Uploaded by

urbanatuba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Java File

Input/Output
CSIT213 Java Programming
Overview
• Files and streams

• Sequential-access text file

• Object serialization

2
Files and Streams
• Java views each file as a sequential stream of bytes.
• Every operating system provides a mechanism to determine the end of a
file, such as an end-of-file marker or a count of the total bytes in the file
that is recorded in a system-maintained administrative data structure.
• A Java program simply receives an indication from the operating
system when it reaches the end of the stream.

3
Files and Streams
• File streams can be used to input and output data as bytes or characters.
- Byte-based streams output and input data in its binary format—a char is
two bytes, an int is four bytes, a double is eight bytes,...
- Character-based streams output and input data as a sequence of
characters in which every character is two bytes—the number of bytes
for a given value depends on the number of characters in that value.
• Files created using byte-based streams are referred to as binary files.
• Files created using character-based streams are referred to as text files. Text
files can be read by text editors.
• Binary files are read by programs that understand the specific content of
the file and the ordering of that content.
- Reading binary files is generally much faster.
4
Files and Streams
• A Java program opens a file by creating an object and associating a stream
of bytes or characters with it.

Can also associate streams with different devices.


• Java creates 3 stream objects when a program begins executing
- System.in (standard input stream object) inputs bytes from the keyboard
- System.out (standard output stream object) outputs character data to
the screen
- System.err (standard error stream object) outputs character-based error
messages to the screen.
• Class System provides methods setIn, setOut and setErr to redirect the
standard input, output and error streams, respectively.
5
Files and Streams
• Java programs perform file processing by using classes from package java.io
and the sub-packages of java.nio

• Character-based input and output can be performed with classes Scanner and
Formatter.
- Class Scanner is used extensively to input data from the keyboard. This class
can also read data from a file.
- Class Formatter enables formatted data to be output to any text-based stream
like method System.out.printf.

• Java SE 8 adds another type of stream


- In week 11, we will introduce a new type of stream that’s used to process collections
of elements (like arrays and ArrayLists), rather than the streams of bytes we discuss
in this chapter’s examples. They are very different from file streams and used with
Lambda expressions.
6
Using NIO Classes and Interfaces
• Interfaces Path and DirectoryStream and classes Paths and Files (all from
package java.nio.file) are useful for retrieving information about files and
directories on disk:

- Path interface: Objects of classes that implement this interface


represent the location of a file or directory. Path objects do not open
files or provide any file-processing capabilities.

- Paths class: Provides static methods used to get a Path object


representing a file or directory location.

7
Using NIO Classes and Interfaces
• Files class: provides static methods for common file and directory
manipulations. Such as copying files, creating and deleting files and
directories, getting information about files and directories, reading the
contents of files, getting objects that allow you to manipulate the
contents of files and directories, and more.

• DirectoryStream interface: Objects of classes that implement this


interface enable a program to iterate through the contents of a directory.

8
Using NIO Classes and Interfaces
• A file or directory’s path specifies its location on a disk. The path includes
some or all the directories leading to the file or directory.

• An absolute path contains all directories, starting with the root directory
that leads to a specific file or directory.

• Every file or directory on a particular disk drive has the same root directory
in its path.

• A relative path is “relative” to another directory—for example, a path


relative to the directory in which the application began executing.

9
Using NIO Classes and Interfaces
• An overloaded version of the Files static method uses a URI object to
locate the file or directory.
• A Uniform Resource Identifier (URI) is a more general form of the Uniform
Resource Locators (URLs) that are used to locate websites.
- On Windows platforms, the URI
file://C:/data.txt
identifies the file data.txt stored in the root directory of the C: drive.
- On UNIX/Linux platforms, the URI
file:/home/student/data.txt
identifies the file data.txt stored in the home directory of the user student.

10
Using NIO Classes and Interfaces
• A separator character is used to separate directories and files in a path.
– On a Windows computer, the separator character is a backslash (\).
– On a Linux or Mac OS X system, it’s a forward slash (/).

• Java processes both characters identically in a pathname.

• For example, if we were to use the path


c:\Program Files\Java\jdk1.6.0_11\demo/jfc
which employs each separator character, Java would still process the path
properly.

11
testFile.java

Example - a file/directory information


import java.util.Scanner;
import java.util.Formatter;
import java.io.IOException;
import java.nio.file.*;

public void dir() {
in = new Scanner(System.in);
System.out.print("Input a file or directory name: ");
String name = in.nextLine();

Path path = Paths.get(name);


try {
if(Files.exists(path)) {

12
Example - a file/directory information
if(Files.isDirectory(path)) {
System.out.println("Directory contains:");
DirectoryStream<Path> ds = Files.newDirectoryStream(path);
for(Path p : ds)
System.out.println(p);
}
else {
System.out.printf("%s is a file.\n", path);
System.out.printf("Last modified: %s\n", Files.getLastModifiedTime(path));
System.out.printf("Size: %s\n", Files.size(path));
System.out.printf("Absolute path: %s\n", path.toAbsolutePath());
}

13
Read data from a sequential-access text file
• Sequential-access files store records in order.

• Text files are human-readable files.

• Java imposes no structure on a file

- Notions such as records do not exist as part of the Java language.

- You must structure files to meet the requirements of your applications.


• Open a text file by using Scanner object.

- If a path is not specified, the JVM assumes that the file is in the
directory from which the program was executed.

14
sampleFile1.txt

Example - Department records in a text file


1, MARKET AND SALES, 110, 1234.00, 02/01/2012
2, ACCOUNTING, 120, 5566789.50, 30/10/2010
3, GAMES DEVELOPMENT, 150, 100000.00, 01/03/2008
4, HUMAN RESOURCES, 200, 500000.0, 02/01/2013
5, SPORTS, 250, 8500000.00, 10/05/2010
6, RESEARCH, 300, 45500.00, 10/06/2020
7, EDUCATION, 350, 100000.00, 10/07/2019
8, FINANCE, 500, 8400000.00, 10/08/2022
9, COMPUTING, 600, 90000.00, 10/09/2018

15
Example - class Department
class Department {
private int number;
private String name;
private int manager;
private double budget;
private String startDate;

public Department(int num, String name, int mnum, double budget, String sDate) {
number = num;
this.name = name;
manager = mnum;
this.budget = budget;
startDate = sDate;
}
public String toString() {
return String.format("%d, %s, %d, %.2f, %s", number, name, manager, budget,
startDate);
}
}

16
testFile.java

Example - read department records


public void readFile() {
System.out.print("Input a text filename: ");
String name = keyboardIn.nextLine();
Path path = Paths.get(name);
try {
if(Files.exists(path)) {
System.out.printf("%s exists\n", path.getFileName());

if(!Files.isDirectory(path)) {
Scanner fin = new Scanner(path);
fin.useDelimiter(", |\r\n|\t|\n");

17
Example - read department records
while(fin.hasNext()) {
int number = fin.nextInt();
String dname = fin.next();
int manager = fin.nextInt();
double budget = fin.nextDouble();
String sDate = fin.next();

Department d = new Department(number, dname, manager, budget, sDate);


departments.add(d);
}
fin.close();

18
Creating a sequential - access text file
• Formatter outputs formatted Strings to the specified stream.

• The constructor of Formatter with one String argument receives the name
of the file, including its path.

- If a path is not specified, the JVM assumes that the file is in the directory
from which the program was executed.

• If the file does not exist, it will be created.

• If an existing file is opened, its contents are truncated.

19
Example - save department records
public void writeFile() {
System.out.print("Input an output filename: ");
String name = keyboardIn.nextLine();
try {
Formatter fout = new Formatter(name);
for(Department d:departments) {
fout.format("%s\n", d);
}
fout.close();
}
catch (IOException err) {
System.out.println("IO exception error");
}
}

20
Object Serialization

• To read an entire object from or write an entire object to a file, Java provides
object serialization.

• A serialized object is represented as a sequence of bytes that includes the


object’s data and its type information.

• After a serialized object has been written into a file, it can be read from the
file and deserialized to recreate the object in memory.

21
Object Serialization

• Classes ObjectInputStream and ObjectOutputStream (package java.io)


which respectively implement the ObjectInput and ObjectOutput
interfaces, enabling entire objects to be read from or written to a stream.

• To use serialization with files, initialize ObjectInputStream and


ObjectOutputStream objects that read from and write to files.

22
Object Serialization
• ObjectOutput interface method writeObject takes an Object as an
argument and writes its information to an OutputStream.

• A class that implements ObjectOuput (such as ObjectOutputStream)


declares this method and ensures that the object being output shall
implement interface Serializable.

• ObjectInput interface method readObject reads and returns a reference


to an Object from an InputStream.
– After an object has been read, its reference can be downcast to the
object’s actual type.

23
Object Serialization
• Objects of classes that implement interface Serializable can be serialized
and deserialized with ObjectOutputStreams and ObjectInputStreams.

• Interface Serializable is a marker interface.


– It does not contain methods.
– Like Cloneable
• A class that implements Serializable is tagged as being a Serializable
object.

• An ObjectOutputStream will not output an object unless it is a Serializable


object.

24
Object Serialization
• In a class that implements Serializable, every variable must be
Serializable.

• Anyone that is not must be declared transient, it will be ignored during the
serialization process.

• All primitive-type variables are serializable.

• For reference-type variables, check the class’s documentation (and possibly


its superclasses) to ensure that the type is Serializable.

25
Object input serialization

• ObjectInputStream method readObject reads an object from a file.

• Method readObject throws an EOFException if an attempt is made to


read beyond the end of the file.

• Method readObject throws a ClassNotFoundException if the class for


the object being read cannot be located.

26
testBFile.java

Example - Serializable
class Student implements Serializable {
private int number;
private String name;
private String dob;
private String email;
private String address;
private String phone;
private String degree;

public String toString() {
return String.format("%d, %s, %s, %s, %s, %s, %s", number, name, dob,
email, address, phone, degree);
}
}

27
Example - output data to a binary file
public void writeBFile() {
System.out.print("Input an output filename: ");
String name = keyboardIn.nextLine();
try {
ObjectOutputStream fout = new
ObjectOutputStream(Files.newOutputStream(Paths.get(name)));

for(Student s:students) {
fout.writeObject(s);
}

fout.close();
}
catch (IOException err) {
System.out.println("IO exception error. " + err);
}
}

28
Example - read data from a binary file
public void readBFile() {
System.out.print("Binary filename: ");
String name = keyboardIn.nextLine();
Path path = Paths.get(name);
ObjectInputStream fin = null;
try {
if(Files.exists(path)) {
if(!Files.isDirectory(path)) {
fin = new ObjectInputStream(Files.newInputStream(path));

while(true) { Infinite loop


Student s = (Student) fin.readObject();

students.add(s);
}
}
29
Example – read data from a binary file
else
System.out.printf("File %s is a directory.\n", path);
}
else
System.out.printf("File %s does not exist.\n", path);
}
catch (EOFException err) {
System.out.println("No more records.");
try {
if(fin != null)
fin.close();
}
catch(IOException e) {
System.out.printf("Close the file %s failed. Error=%s\n", path, e);
}
}

30
Example – read data from a binary file
catch (ClassNotFoundException err) {
System.out.println("Invalid object type. " + err);
}
catch (IOException err) {
System.out.println("IO exception error. " + err);
}
}

31

You might also like