OOP Through Java
Unit IV part 2
JAVA I/O:Streams and Files
JavaI/O and File: Java I/O API, standard I/Ostreams types, Byte streams, Character streams, Scanner
class, Files in Java
------------------------------------------------------------------------------------------------------------------------------------
I/O
The Java I/O (Input/Output) API provides a robust set of tools for reading and writing data to
various input and output sources such as files, memory buffers, and network connections.
Java’s I/O system consists of:
1. Byte Streams: For handling binary data.
2. Character Streams: For handling character data (text).
3. Scanner Class: For parsing input, typically from the console or files.
4. File Class: For file manipulation, such as checking if a file exists, creating directories,
etc.
Data is transferred to devices by ‘streams‘
Streams
JAVA distinguishes between 2 types of streams:
1. Text Streams, containing ‘characters‘
2. Binary Streams, containing 8-bit information
Streams in JAVA are Objects
Having
• 2 types of streams (text / binary) and
• 2 directions (input / output)
results in 4 base-classes dealing with I/O:
1. Reader: text-input
2. Writer: text-output
3. InputStream: byte-input
4. OutputStream: byte-output
• InputStream, OutputStream, Reader, Writer are abstract classes
I/O: General Scheme
In General:
Reading (writing):
– open an input (output) stream
– while there is more information
read(write) next data from the stream
– close the stream.
In JAVA:
– Create a stream object and associate it with a disk-file
– Give the stream object the desired functionality
– while there is more information
read(write) next data from(to) the stream close the stream.
Example 1
Writing a Text
Writing Textfiles
For writing text files we use,
Class: FileWriter
Frequently used methods:
Using FileWriter
• is not very convenient (only String-output possible)
• Is not efficient (every character is written in a single step, invoking a huge overhead)
Better: wrap FileWriter with processing streams
• BufferedWriter
• PrintWriter
Wrapping Textfiles
BufferedWriter:
• Buffers output of FileWriter, i.e. multiple characters are processed together, enhancing
efficiency
PrintWriter
• provides methods for convenient handling, e.g. println()
( remark: the System.out.println() – method is a method of the PrintWriter-instance System.out ! )
Wrapping a Writer
A typical code segment for opening a convenient, efficient textfile:
FileWriter out = new FileWriter("test.txt");
BufferedWriter b = new BufferedWriter(out);
PrintWriter p = new PrintWriter(b);
Or with anonymous (‘unnamed‘) objects:
PrintWriter p = new PrintWriter( new BufferedWriter(new FileWriter("test.txt")));
Reading Textfiles
Class: FileReader
Frequently used Methods:
Wrapping a Reader
Using FileReader is not very efficient. Better
wrap it with BufferedReader:
BufferedReader br = new BufferedReader(new FileReader(“name“));
EOF Detection
Detecting the end of a file (EOF):
• Usually amount of data to be read is not known
• Reading methods return ‘impossible‘ value if end of file is reached
• Example:
– FileReader.read returns -1
– BufferedReader.readLine() returns ‘null‘
• Typical code for EOF detection:
while ((c = myReader.read() != -1){ // read and check c
...do something with c
}
Example : Copying a Textfile
import java.io.*;
public class IOTest
{
public static void main(String[] args)
{
try{
BufferedReader myInput = new BufferedReader(new
FileReader("IOTest.java"));
BufferedWriter myOutput = new BufferedWriter(new
FileWriter("Test.txt"));
int c;
while ((c=myInput.read()) != -1)
myOutput.write(c);
myInput.close();
myOutput.close();
}
catch(IOException e){}
}
}
Binary Files
• Stores binary images of information identical to the binary images stored in main memory
• Binary files are more efficient in terms of processing time and space utilization
• drawback: not ‘human readable‘, i.e. you can‘t use a texteditor (or any standard-tool) to
read and understand binary files
Example: writing of the integer ’42‘
• TextFile: ‘4‘ ‘2‘ (internally translated to 2 16-bit representations of the characters ‘4‘ and ‘2‘)
• Binary-File: 00101010, one byte (= 42 decimal)
Writing Binary Files
Class: FileOutputStream
No difference in usage as FileWriter, only in output format
Reading Binary Files
Class: FileInputStream
No difference in usage as FileReader, only in output format
Binary vs. TextFiles
**********************************************************************************
****