Input/output
Basics-
The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports hu many data such as primitives, object, localized characters, etc.
Stream-
A stream can be defined as a sequence of data. There are two kinds of Streams
- InPutStream − The InputStream is used to read data from a source.
- OutPutStream − The OutputStream is used for wriÆŸng data to a destination.
Java provides strong but flexible support for I/O related to files and networks but this tutorial covers very basic functionality related to streams and I/O. We will see the most commonly used examples one by one −
Byte and character stream-
Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file-
Example
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Character Streams-
Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
We can re-write the above example, which makes the use of these two classes to copy an input file (having unicode characters) into an output file −
Example
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Predefined streams-
- As you know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. Among other things, it contains three predefined stream variables, called in, out, and err. These fields are declared as public, final, and static within System. This means that they can be used by any other part of your program and without reference to a specific System object.
Reading and writing from console and file-
As described earlier, a stream can be defined as a sequence of data.
The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Here is a hierarchy of classes to deal with Input and Output streams.
The two important streams are FileInputStream and FileOutputStream, which would be discussed in this tutorial.
FileInputStream
This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream
object to read the file −
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows −
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.
Basics-
- The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
- Networking is the concept of connecting multiple remote or local devices together. Java program communicates over the network at application layer.
Networking classes and interfaces-
- Java supports TCP/IP both by extending the already established stream I/O interface introduced in Chapter 19 and by adding the features required to build I/O objects across the network. Java supports both the TCP and UDP protocol families. TCP is used for reliable stream based I/O across the network. UDP supports a simpler, hence faster, point-to- point datagram-oriented model. The classes contained in the java.net package are shown here:
- Authenticator
- Inet6Address
- Ser verSocket
- CacheRequest
- InetAddress
- Socket
- CacheResponse
- InetSocketAddress
- SocketAddress
- ContentHandler
- Inter faceAddress (Added by SocketImpl
- Java SE 6.)
- CookieHandler
- JarURLConnection
- SocketPermission
- CookieManager (Added by MulticastSocket
- URI
- Java SE 6.)
- DatagramPacket
- NetPermission
- URL
Using java.net package-
The java.net package of the J2SE APIs contains a collection of classes and
interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand.
The java.net package provides support for the two common network protocols −
- TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.
- UDP − UDP stands for User Datagram Protocol, a connecÆŸon-less protocol that allows for packets of data to be transmitted between applications.
Next Unit- Event Handling
Tags
Java