You are on page 1of 11

ADVANCED JAVA: I/O

OPERATIONS
I/O, NEW I/O
JAVA I/O
Java SE provides two kinds of basic packages for
performing I / O operations:
java.io - provides classes responsible for
handling streams
java.nio - introduces new types of input/output,
including channels, buffers, selectors
I/O - BYTE STREAMS
All the classes related to byte streams are derived
from the InputStream and OutputStream classes,
e.g. FileInputStream,FileOutputStream in the
java.io package.

FileInputStream in = new
FileInputStream("user.txt");

FileOutputStream out = new


FileOutputStream("user_output.txt");

int c;

while ((c = in.read()) != -1) {

out.write(c);

}
I/O - CHARACTER STREAMING
Streaming classes are derived from the Reader and
Writer classes. As with byte streams, there are
character stream classes that specialize in I/O files:
FileReader and FileWriter.

FileReader = in = new
FileReader("user.txt");

FileWriter out = new


FileWriter("user_output.txt");

int nextChar;

while ((nextChar = in.read()) != -1) {

out.append((char) nextChar);

}
I/O - DATA BUFFERING
The program can convert an unbuffered stream to a
buffered stream by using classes such as:
BufferedReader, BufferedWriter, which enable
character streams to be buffered, and
BufferedInputStream and
BufferedOutputStream enable byte streaming.

BufferedReader in = new
BufferedReader(new FileReader("user.txt"));

BufferedWriter out = new


BufferedWriter(new
FileWriter("user_output.txt"));

String line;

while ((line = in.readLine()) != null) {

out.write(line);

}
JAVA NIO - BUFFERS AND CHANNELS
A buffer is a data container of a specific primitive type.
A buffer is a linear, finite sequence of elements of a
certain primitive type. Buffers in java nio are handled
by the Buffer class (from the java.nio package).
A channel (from the java.nio.channels.Channel
package) represents an open connection to an entity,
such as a hardware device, file, network socket, or
program component.
JAVA NIO - BUFFERS AND CHANNELS

CharBuffer buffer =
CharBuffer.allocate(8);

String text = "sda";

System.out.println("Input text: " +


text);

for (int i = 0; i < text.length(); i++) {

char c = text.charAt(i);

buffer.put(c);

System.out.println("Position after data


is written into buffer: " +
Arrays.toString(buffer.array()));
JAVA NIO - PATH
The Path class contains methods for performing basic
file operations.

Path path = Paths.get("data.txt");

Files.createFile(path);

Files.write(path, "A long time ago in a


galaxy far, far away....".getBytes(),
StandardOpenOption.WRITE);

Files.write(path, "in a galaxy far,


".getBytes(), StandardOpenOption.APPEND);

Files.write(path, "far
away....\n".getBytes(),
StandardOpenOption.APPEND);

for (String line :


Files.readAllLines(path)) {

System.out.println(line);

Files.delete(path);
SERIALIZATION AND DESERIALIZATION
Serialization is the process of converting an object's
state into a stream of bytes, while deserialization
reverses the process.
SERIALIZABLE
Interface from the java.io package that enables
object serialization and deserialization. Without this
interface, classes of the type: ObjectInputStream,
ObjectOutputStream will not be able to perform
both conversion processes.

public class Book implements Serializable {

private int id;

private String title;

private String author;

You might also like