advanced 2
advanced 2
1
Objectives
To elaborate files and streams
2
1. Introduction
Program often need to communicate with the outside world.
3
I/O Overview:
I/O = Input/Output
The data source and data destination can be anything that holds, generates, or
consumes data. Obviously this includes disk files, a peripheral device, a
network socket, or an array.
4
2. Files and Streams
Data stored in variables and arrays is temporary.
The program does not need to know how the underlying platform represents
files or streams.
6
2.1 Types of Streams
Text files can be read by text editors, while binary files are
read by programs that understand the file’s specific content
and its ordering for changing to human readable format.
8
Binary Vs. Text Files:
All data and programs are ultimately just zeros and ones
Text files: the bits represent printable characters
for example, Java source files are text files
so is any file created with a “text editor”
Text files are more readable by humans
Binary files: the bits represent other types of encoded
information, such as executable instructions or numeric data
these files are easily read by the computer but not humans
The object’s constructor interacts with the operating system to open the
file.
Java can also associate streams with different devices. When a Java
program begins executing, in fact, it creates three stream objects that are
associated with devices—System.in, System.out and System.err.
10
Object System.out (the standard output stream object) normally
enables a program to output character data to the screen; and
11
Reading information into a program: A program uses an input stream to
read data from a source, one item at a time.
12
Java programs perform file processing by using classes from
2.3 The java.io Package
package java.io
Memory Disk
smiley.txt
smileyOutStream
However, File objects are used frequently with objects of other java.io
classes to specify files or directories to manipulate.
An absolute path contains all the directories, starting with the root
directory, that lead to a specific file or directory. (C:/home/index.html)
A relative path normally starts from the directory in which the application
began executing and is therefore “relative” to the current directory.
(index.html or home/index.html)
The constructor with File and String arguments uses an existing File object
that specifies the parent directory of the file or directory specified by the
String argument.
The fourth constructor uses a URI object to locate the file. A Uniform
Resource Identifier (URI) is amore general form of the Uniform Resource
Locators (URLs) that are used to locate websites.
16
4. Sequential-Access Text Files
Java imposes no structure on a file. You must structure
files to meet your application’s needs.
18
5. Object Serialization
Java provides a mechanism called object serialization that
enables entire objects to be written to a file.
A serialized object is represented as a sequence of bytes that
includes the object’s data as well as information about the
object’s type and the types of data it stores.
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.
Classes ObjectInputStream and ObjectOutputStream enable
entire objects to be read from or written to a stream.
19Only classes that implement interface Serializable can be
Example 1: Create and Write to a file by using File and
PrintWriter classes
package fileio;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
output.println("G3Comp");
output.println("A,B & C");
output.close(); //always we need to close opened file
} catch (IOException ex) {
System.out.printf("ERROR:%s\n", ex);
20
}
Example 2: Read from a file by using Scanner class
package fileio;
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
try {
Scanner read = new Scanner(file); //Read some thing from test.txt that referenced by file object
String name1=read.nextLine();
String name2=read.nextLine();
read.close(); //always we need to close opened file
System.out.print(name1);
System.out.print(name2);
} catch (IOException ex) {
21
System.out.printf("ERROR:%s\n", ex);
Example 3: Write to a file by using FileWriter class
package fileprocess;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
23