Streams & IO

Program for reading data from keyboard using DataInputStream

DataInputStream is a FilterInputStream that read primitive data from an underlying input stream in a machine-independent way.

It implements the DataInput interface and provides convenience methods such as readInt(), readChar etc.

It also inherits the methods from its immediate parent FilterInputStream.

Below program will take the input from keyboard using DataInputStream with its inherited read method and print it to a file using FileOutputStream.

 

Code

import java.io.*;

 

public class FileCopier {

Program for copying file contents using BufferedReader and BufferedWriter along with FileReader and FileWriter

To improve the efficiency we can use Buffered classes in connection with other streams. 

BufferedReader adds the ability to buffer the input and to support the mark and reset methods.

Similarly a BufferedWriter writes to the file using FileWriter only when the buffer is full, not for every byte, and this reduces the actual file writes and hence improve efficiency.

Program for copying file contents using FileReader and FileWriter

FileReader and FileWriter are convenience classes when working with character files. The first program using FileInputStream and FileOutputStream can be changed to use FileReader and FileWriter, by simply replacing FileInputStream with FileReader and FileOutputStream with FileWriter.

 

Code

import java.io.*;

 

public class FileCopier {

Program for copying file contents using FileInputStream and FileOutputStream

This program will read a file using FileInputStream and prints it to another file using FileOutputStream.

FileInputStream obtains input bytes from a file in a file system. 

FileInputStream is a low level byte stream, whose constructor takes the name of the file as an argument.

FileOutputStream is also another low level byte stream, whose constructor takes the name of the file as an argument.

 

Code

import java.io.*;

 

public class FileCopier {

Java IO Notes

We will discuss about Java I/O, File I/O, NIO2 and related topics here.

List of Stream Class Constructors for Quick Reference

This is a quick reference of constructors for various Stream classes.

You can quickly find out what all type of resources or streams a Stream class can be attached with. For instance, a BufferedReader do not have a constructor that can accept an InputStream, however an InputStreamReader has such a constructor that can accept an InputStream. So to use an InputStream such as System.in, we can attach it to an InputStreamReader and then attach the InputStreamReader to a BufferedReader as:

Introduction to Streams in Java

A stream can be considered as a sequence of bytes travelling from a source to a destination.

All input and output data transfer in Java happens through streams.

There can be different types of sources and destinations such as disk files, input devices like keyboards and even other programs, and there are different stream classes available corresponding to each of these types of sources and destinations.

Best Practices for Serialization in Java

This article lays down some of the best practices which you can use during your design or code reviews, and assumes that you are familiar with the basics of serialization in java. Consider using custom serialized form and don’t use the default serialized form provided by Java even if you have decided to design only the APIs perfectly for time being releasing a throwaway implementation and then replace it with a better one in future release.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!