View RSS Feed

My Java Tips

Byte Streams

Rate this Entry
by , 11-11-2011 at 05:27 PM (2032 Views)
Java IO programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.


There are many byte stream classes. To demonstrate how byte streams work, we'll focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. A FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time.

Below is the list of the FileInputStream class constructors.

FileInputStream(File file)
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

FileInputStream(FileDescriptor fdObj)
Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

FileInputStream(String name)
Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

Below is the list of methods of the FileInputStream class.

int available()
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

void close()
Closes this file input stream and releases any system resources associated with the stream.

protected void finalize()
Ensures that the close method of this file input stream is called when there are no more references to it.

FileChannel getChannel()
Returns the unique FileChannel object associated with this file input stream.

FileDescriptor getFD()
Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.

int read()
Reads a byte of data from this input stream.

int read(byte[] b)
Reads up to b.length bytes of data from this input stream into an array of bytes.

int read(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes.

long skip(long n)
Skips over and discards n bytes of data from the input stream.

Below is the list of the FileOutputStream class constructors.

FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object.

FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.

FileOutputStream(FileDescriptor fdObj)
Creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

FileOutputStream(String name)
Creates an output file stream to write to the file with the specified name.

FileOutputStream(String name, boolean append)
Creates an output file stream to write to the file with the specified name.

Below is the list of methods of the FileOutputStream class.

void close()
Closes this file output stream and releases any system resources associated with this stream.

protected void finalize()
Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.

FileChannel getChannel()
Returns the unique FileChannel object associated with this file output stream.

FileDescriptor getFD()
Returns the file descriptor associated with this stream.

void write(byte[] b)
Writes b.length bytes from the specified byte array to this file output stream.

void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this file output stream.

void write(int b)
Writes the specified byte to this file output stream.

Example

This program reads data in a file and displays it as it is on console. It uses read() method to read data and write() to copy the data. To use this program, specify the name of the file that you want to see. For example, to see a file called DisplayAndCopyFile.java and copy it to abc.txt, use the following at command line: java DisplayAndCopyFile DisplayAndCopyFile.java abc.txt

Java Code:
import java.io.*;

public class DisplayAndCopyFile {
	public static void main(String args[]) throws IOException {
		int i;
		FileInputStream fin;
		FileOutputStream fout;

		try {
			// open input file
			try {
				fin = new FileInputStream(args[0]);
			} catch (FileNotFoundException e) {
				System.out.println("Input File Not Found");
				return;
			}

			// open output file
			try {
				fout = new FileOutputStream(args[1]);
			} catch (FileNotFoundException e) {
				System.out.println("Error Opening Output File");
				return;
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Usage: DisplayAndCopyFile From To");
			return;
		}

		// Display and Copy File
		try {
			do {
				i = fin.read();
				if (i != -1)
					System.out.print((char) i);
				if (i != -1)
					fout.write(i);
			} while (i != -1);
		} catch (IOException e) {
			System.out.println("File Error");
		}

		fin.close();
		fout.close();
	}
}

Submit "Byte Streams" to Facebook Submit "Byte Streams" to Digg Submit "Byte Streams" to del.icio.us Submit "Byte Streams" to StumbleUpon Submit "Byte Streams" to Google

Tags: byte, java io, streams Add / Edit Tags
Categories
Streams

Comments