Professional Documents
Culture Documents
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");
int c;
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");
int nextChar;
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"));
String line;
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);
char c = text.charAt(i);
buffer.put(c);
Files.createFile(path);
Files.write(path, "far
away....\n".getBytes(),
StandardOpenOption.APPEND);
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.