06-Java Basic IO
06-Java Basic IO
IO and Streams
Objectives
java.io package
Text, UTF, and Unicode
File Input and Output
Streams, Readers, and Writers
Object Streams and Serialization
Text, UTF, and Unicode
ASCII code
256 characters
(8 bits)
Character
Unicode 65536 characters
(16 bits) ( not completely represented)
Java :
Uses UTF to read/write Unicode
Helps converting Unicode to external 8-bit encodings and
vice versa.
Text files
Input stream
Output stream
Streams (2)
if(!f.exists()) f.createNewFile();
if(!f.exists()) f.delete();
Where:
mode: should be either “r” or “rw”. Use “r” to open the
file for reading only, and use “rw” to open for both
reading and writing.
RandomAccessFile…
RAF offer the following functionality:
Seeking to any position within a file
Reading and writing single or multiple bytes
Reading and writing groups of bytes, treated as
higher-level data types
Closing
RandomAccessFile methods
long getFilePointer()
long length()
void seek(long position)
int read()
int read(byte dest[])
int read(byte dest[], int offset, int len)
void write(int b)
void write(byte b[])
void write(byte b[],int offset, int len)
void writeBytes(String s)
void close() throws IOException
RandomAccessFile methods…
RandomAccessFile examples (1)
RandomAccessFile f = new
RandomAccessFile("test.txt","rw");
f.writeInt(5);
f.writeDouble(15.5);
f.writeBytes("ABC XYZ");
f.close();
f = new RandomAccessFile("test.txt","r");
int k; double x;String s;
k = f.readInt();
x = f.readDouble();
s = f.readLine();
f.close();
RandomAccessFile examples (2)
RandomAccessFile f = new
RandomAccessFile(“vehicle.dat","r");
String [] a; String s; int year; double cost; Vehicle v;
while(true)
{s = f.readLine();
if(s==null || s.length()<3) break; Madaz123; 2016; 1234; red
civic345;2017;234;yellow
a = s.split("\\s*\\;\\s*"); toyota23; 2018;456;black
if(a==null || a.length<4) break;
year = Integer.parseInt(a[1]);
cost = Double.parseDouble(a[2]);
v = new Vehicle(a[0],year,cost,a[3]);
list.add(v);
}
f.close();
Streams, Readers, and Writers
Java’s stream, reader, and writer classes view
input and output as ordered sequences of bytes.
Dealing strictly with bytes would be tremendously
bothersome, because data appears sometimes
as bytes, sometimes as ints, sometimes as floats,
and so on.
Output
FileInputStream in = null;
DataInputStream datain = null;
try {
in = new FileInputStream(file);
datain = new DataInputStream(in)
System.out.println(datain.readInt());
System.out.println(datain.readDouble());
System.out.println(datain.readChar());
} catch (IOException e) {
A chain of input
streams
Readers and Writers
Readers and writers are like input and output
streams: The low-level varieties communicate
with I/O devices, and the high-level varieties
communicate with low-level varieties.
Readers and writers are exclusively oriented
to Unicode characters.
Readers and Writers..
FileReader and FileWriter
CharArrayReader and CharArrayWriter
PipedReader and PipedWriter
StringReader and StringWriter
BufferedReader and BufferedWriter
InputStreamReader and OutputStreamWriter
LineNumberReader
..
Object Streams and Serialization
Object streams go one step beyond data streams
by allowing you to read and write entire objects.
Serialization
You can also read and write objects to files
Object I/O goes by the awkward name of
serialization
Serialization in other languages can be very
difficult, because objects may contain references
to other objects
Java makes serialization (almost) easy
Conditions for serializability
If an object is to be serialized:
The class must be declared as public
The class must implement Serializable interface
The class must have a no-argument constructor
All fields of the class must be serializable: either
primitive types or serializable objects (static fields
and transient fields are also not serialized)
Implementing the Serializable interface
myObject = (itsType)ob.readObject( );
ob.close( );
Example (Vidu3DT)
Summary
java.io package
Text, UTF, and Unicode
File Input and Output
Streams, Readers, and Writers
Object Streams and Serialization
Q&A