Showing posts with label IO / Input Output. Show all posts
Showing posts with label IO / Input Output. Show all posts

Monday, 16 January 2012

Java IO Overview

We can diagrammatically cover how java io looks like from bird eye view.

So this is basic java IO :
Now looking at the class hierarchy :

Friday, 13 May 2011

Classes helping in parsing the input

Some of the classes in the Java IO API are designed to help you parse input. These classes are:
  • PusbackInputStream
  • StreamTokenizer
  • PushbackReader
  • LineNumberReader
It is not the purpose of this text to give you a complete course in parsing of data. The purpose was rather to give you above quick list of classes related to parsing of input data.
If you have to parse data you will often end up writing your own classes that use some of the classes in this list. I know I did when I wrote the parser for the Butterfly Container Script. I used the PushbackInputStream at the core of my parser, because sometimes I needed to read ahead a character or two, to determine what the character at hand meant.

Tuesday, 10 May 2011

Java - Copying one file to another

Here is function called copyFile  which takes 2 strings – source and destination. It copies source to destination.

 

private static void copyFile(String srcFile, String dstFile){

try{

File f1 = new File(srcFile);

File f2 = new File(dstFile);

InputStream in = new FileInputStream(f1);

//Append the file
OutputStream out = new FileOutputStream(f2,true);
//Overwrite the file.
OutputStream out = new FileOutputStream(f2);


byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

in.close();

out.close();

System.out.println("File copied.");

}

catch(FileNotFoundException ex){

System.out.println(ex.getMessage() + " in
the specified directory."
);
return false;

}

catch(IOException e){

System.out.println(e.getMessage());
return false;

}
return true;
}

Monday, 9 May 2011

Standard Streams in java

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

Java also supports three Standard Streams:

  • Standard Input: Accessed through System.in which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

Using DataInputStream and DataOutputStream


Serialization tutorial index


Working with Filtered Streams

You attach a filtered stream to another stream to filter the data as it's read from or written to the original stream. The java.io package contains these filtered streams which are subclasses of either FilterInputStream(in the API reference documentation) or FilterOutputStream(in the API reference documentation):

  • DataInputStream and DataOutputStream
  • BufferedInputStream and BufferedOutputStream
  • LineNumberInputStream
  • PushbackInputStream
  • PrintStream (this is an output stream)
This section shows you how to use filtered streams through an example that uses a DataInputStream and a DataOutputStream. In addition, this section shows you how to write your own filtered streams.
Using Filtered Streams
To use a filtered input or output stream, attach the filtered stream to another input or output stream. For example, you can attach a DataInputStream to the standard input stream as in the following code:
DataInputStream dis = new DataInputStream(System.in);
String input;

while ((input = dis.readLine()) != null) {
. . . // do something interesting here
}
You might do this so that you can use the more convenient readXXX methods, such as readLine, implemented by DataInputStream.

How to use pipe stream


Using the Processing Streams

Processing streams perform some sort of operation, such as buffering or character encoding, as they read and write. Like the data sink streams, java.io often contains pairs of streams: one that performs a particular operation during reading and another that performs the same operation (or reverses it) during writing. This table gives java.io's processing streams.
 

Process CharacterStreams Byte Streams
Buffering BufferedReader,
BufferedWriter
BufferedInputStream,
BufferedOutputStream
Filtering FilterReader,
FilterWriter
FilterInputStream,
FilterOutputStream
Converting between
Bytes and Characters
InputStreamReader,
OutputStreamWriter
 
Concatenation   SequenceInputStream
Object Serialization   ObjectInputStream,
ObjectOutputStream
Data Conversion   DataInputStream,
DataOutputStream
Counting LineNumberReader LineNumberInputStream
Peeking Ahead PushbackReader PushbackInputStream
Printing PrintWriter PrintStream

Notice that many times, java.io contains character streams and byte streams that perform the same processing but for the different data type. The processing streams are briefly described here:

BufferedReader and BufferedWriter BufferedInputStream and BufferedOutputStream
Buffer data while reading or writing, thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar nonbuffered streams.
FilterReader and FilterWriter FilterInputStream and FilterOutputStream
Abstract classes, like their parents. They define the interface for filter streams, which filter data as it's being read or written. Working with Filter Streams later in this lesson shows you how to use filter streams and how to implement your own.
InputStreamReader and OutputStreamWriter
A reader and writer pair that forms the bridge between byte streams and character streams. An InputStreamReader reads bytes from an InputStream and converts them to characters using either the default character-encoding or a character-encoding specified by name. Similarly, an OutputStreamWriter converts characters to bytes using either the default character-encoding or a character-encoding specified by name and then writes those bytes to an OutputStream. You can learn the name of the default character-encoding by calling System.getProperty("file.encoding"). Find out more about character-encoding in the Internationalization trail.
SequenceInputStream
Concatenates multiple input streams into one input stream. How to Concatenate Files has a short example of this class.
ObjectInputStream and ObjectOutputStream
Used to serialize objects. See Object Serialization.
DataInputStream and DataOutputStream
Read or write primitive Java data types in a machine-independent format. How to Use DataInputStream and DataOutputStream shows you an example of using these two streams.
LineNumberReader LineNumberInputStream
Keeps track of line numbers while reading.
PushbackReader PushbackInputStream
Two input streams each with a 1-character (or byte) pushback buffer. Sometimes, when reading data from a stream, you will find it useful to peek at the next item in the stream in order to decide what to do next. However, if you do peek ahead, you'll need to put the item back so that it can be read again and processed normally.
PrintWriter PrintStream
Contain convenient printing methods. These are the easiest streams to write to, so you will often see other writable streams wrapped in one of these.

Using the Data Sink Streams

Data sink streams read from or write to specialized data sinks such as strings, files, or pipes. Typically, for each reader or input stream intended to read from a specific kind of input source, java.io contains a parallel writer or output stream that can create it. The following table gives java.io's data sink streams.

Sink Type Character Streams Byte Streams
Memory CharArrayReader,
CharArrayWriter
ByteArrayInputStream,
ByteArrayOutputStream
StringReader,
StringWriter
StringBufferInputStream
Pipe PipedReader,
PipedWriter
PipedInputStream,
PipedOutputStream
File FileReader,
FileWriter
FileInputStream,
FileOutputStream

 

Note that both the character stream group and the byte stream group contain parallel pairs of classes that operate on the same data sinks. These are described next:

CharArrayReader and CharArrayWriter
ByteArrayInputStream and ByteArrayOutputStream
Use these streams to read from and write to memory. You create these streams on an existing array and then use the read and write methods to read from or write to the array.
FileReader and FileWriter
FileInputStream and FileOutputStream
Collectively called file streams, these streams are used to read from or write to a file on the native file system. How to Use File Streams has an example that uses FileReader and FileWriter to copy the contents of one file into another.
PipedReader and PipedWriter
PipedInputStream and PipedOutputStream
Implement the input and output components of a pipe. Pipes are used to channel the output from one program (or thread) into the input of another. See PipedReader and PipedWriter in action in How to Use Pipe Streams.
StringReader and StringWriter
StringBufferInputStream
Use StringReader to read characters from a String as it lives in memory. Use StringWriter to write to a String. StringWriter collects the characters written to it in a StringBuffer, which can then be converted to a String. StringBufferInputStream is similar to StringReader, except that it reads bytes from a StringBuffer.

Where do streams come from?

System.out is an OutputStream; specifically it's a PrintStream. There's a corresponding System.in which is an InputStream used to read data from the console.

Data for streams can also come from files. Later you'll see how to use the File class and the FileInputStream and FileOutputStream classes to read and write data from files.

Network connections commonly provide streams. When you connect to a web or ftp or some other kind of server, you read the data it sends from an InputStream connected from that server and write data onto an OutputStream connected to that server.

Java programs themselves produce streams. ByteArrayInputStreams, ByteArrayOutputStreams, StringBufferInputStreams, PipedInputStreams, and PipedOutputStreams all use the stream metaphor to move data from one part of a Java program to another.

Perhaps a little surprisingly, components like TextArea do not produce streams. However you can always use the strings they do produce to create a ByteArrayInputStream or a StringBufferInputStream.

The Stream Classes

Most stream classes are part of the java.io package. (There are also a few more in the sun.io and sun.net packages, but those are deliberately hidden from you. There are also a couple in java.util.zip.)
 

Dividing the hierarchy in CharacterStream and ByteStream

As we saw  how type of stream is reflected in java.io hierarchy. So we have java.io defined in this way.  So we have Character Streams as well as ByteStreams.
From point of view of Character Stream, java.io. Reader and java.io. Writer are two main classes.
The two main classes from point of view of byte stream are java.io.InputStream and java.io.OutputStream.
 

CharacterStream

Reader and Writer are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers--streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters.
 
Subclasses of Reader and Writer implement specialized streams and are divided into two categories: those that read from or write to data sinks (shown in gray in the following figures) and those that perform some sort of processing (shown in white). The figure shows the class hierarchies for the Reader and Writer classes.
reader
writer

Most programs should use readers and writers to read and write information. This is because they both can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).

Byte Stream

Programs should use the byte streams, descendants of InputStream and OutputStream, to read and write 8-bit bytes. InputStream and OutputStream provide the API and some implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.

As with Reader and Writer, subclasses of InputStream and OutputStream provide specialized I/O that falls into two categories: data sink streams and processing streams.

inputstream

outputstream

Java IO : Introduction

In Java, input and output is defined in terms of an abstract concept called "stream".  A stream is a sequence of data.  If it is an input stream, it has source.  If it is an output stream, it has a destination.  There are two kinds of streams: byte streams and character streams.  The java.io package provides a large number of classes to perform stream I/O. 
So lets first clear what is stream?

Sunday, 24 April 2011

The InputStream Class

java.io.InputStream is an abstract class that contains the basic methods for reading raw bytes of data from a stream. Although InputStream is an abstract class, many methods in the class library are only specified to return an InputStream, s o you'll often have to deal directly with only the methods declared in this class.
 public abstract int read() throws IOException

public int read(byte[] data) throws IOException
public int read(byte[] data, int offset, int length)
throws IOException

public long skip(long n) throws IOException
public int available() throws IOException
public void close() throws IOException

public void mark(int readLimit)
public void reset() throws IOException
public boolean markSupported()
Notice that almost all these methods can throw an IOException. This is true of pretty much anything to do with input and output. The only exception is the PrintStream class which eats all exceptions.

What is stream in java IO?

Stream
A stream is a sequence of data of undetermined length. It's called a stream because it's like a stream of water that continues to flow. There's no definite end to it.

A better analogy might be a queue of people waiting to get on a ride at an amusement park. As people are processed at the front (i.e. get on the roller coaster) more are added at the back of the line. If it's a slow day the roller coaster may catch up with the end of the line and have to wait for people to board. Other days there may always be people in line until the park closes. Each person is a discrete individual and must be put on the roller coaster or not put on the roller coaster. There are no half-people. There's always a definite number of people in line though this number may change from moment to moment as people enter at the back of the line and exit from the front of the line. Although all the people are discrete, you'll sometimes have a family that must be put together in the same car. Thus although the individuals are discrete, they aren't necessarily unrelated.

In Java a stream is composed of discrete bytes. The bytes may represent chars or other kinds of data. They may come faster than you can handle them, or your thread may block while waiting for the next one to arrive. It often doesn't matter.
The key to processing the stream is a while loop that processes each piece of data, until you encounter the end-of-stream character or some other exceptional condition occurs. In Unix you type end-of-stream with Control-D. On Windows, you type end-of-stream with Control-Z.
 

The Stream Classes


 

Type of Steams in java (reflected in java.io class hierarchy )

The java.io package contains classes that perform input and output. In Java, I/O classes are differentiated according to the type of data being read or written. Byte oriented and numeric data is written with output streams and read with input streams. Character data, that is text, is written with writers and read with readers. Whether you use streams or readers and writers depends on the type of data you're dealing with. All text data, especially non-ASCII text, should be passed through a reader or writer.
The two main stream classes are java.io.InputStream and java.io.OutputStream. The two main reader and writer classes are java.io.Reader and java.io.Writer These are abstract base classes for many different subclasses with more specialized abilities.

Wednesday, 13 April 2011

Different File Read Write Stream Classes in Java

Byte streams, character streams, buffered streams, data streams and object streams are different data reading mechanisms for File IO. Each of the stream has a unique purpose to serve. In this article, we explore details of these streams. Also, we try to find when to use which stream.

fileioclasses

Byte Streams:

java.io.FileInputStream and java.io.FileOutputStream are the two classes for reading and writing purpose from/to a file. These classes extend from InputStream and OutputStream respectively. Data read by these classes is in low level format i.e. binary format. Hence if you have following code, then it will return value as int (which is the converted byte).

FileInputStream ins = new FileInputStream(“myfile.txt”);

int c = ins.read();


Similarly FileOutputStream writes data in binary format. Hence when you really want to deal with file contents in binary mode, such as bit wise operations, logical operations etc., then go for these classes. Otherwise it is better to choose from other file readers/writers.

Character Streams:

Here everything is handled in terms of characters. java.io.FileReader and java.io.FileWriter are the classes that handle character input from files and character output to the file. These classes are subclasses of java.io.Reader and java.io.Writer respectively.Any data read or written is converted into characters. Character encoding becomes important here otherwise you may end up in garbage conversion of some of the characters not available in default encoding, which is generally 8-bit superset of ASCII. As we are dealing with file in character mode, it is possible to handle contents line by line. Hence FileReader.readLine() method is available to read a line. Similarly FileWriter.write method is there to write a line to file.

All above classes read/write data directly from the underlying operation system. Hence each read/write call involves interaction with native APIs and subsequently with underlying operating system. This means network access, disc access, etc. This is not very performing implementation. Hence following set of classes are the solution to this problem.

Buffered Streams:

Buffered I/O is different because it is based on reading/writing data from memory (called as buffer) instead of directly from the underlying system. Now the interaction with disc is dependent on when you want more in buffer, or when you want to flush something out of buffer. By this mechanism we can control the number of calls being made to the underlying operating system.We can also wrap above classes by buffered readers to avail buffering facility. Code snippet like this will help us to get advantage of buffering with previous classes also.

bufferedInput = new BufferedReader(new FileReader(“myfile.txt”));


Output classes mostly support autoflush to empty buffer on disc periodically. Classes like PrintWriter also provide manual flushing ability so that we can optimize usage of these classes. You can find this feature effectively implemented in large response from client to server using http request-response model.

Data Streams:

These classes have got special function to do. Data stream classed provide special methods to read/write primitive data types. If we do not handle primitive data types in a special way and treat them as objects, then there is possibility of data loss during automatic conversions. To avoid this, these classes can be used. java.io.DataInputStream and java.io.DataOutputStream are two important classes. DataInputStream exposes methods like readInt(), readDouble(), etc. to read primitives, while DataOutputStream offeres similar methods writeInt(), writeDouble(), etc. to write primitives.

Object Streams:

java.io.ObjectInputStream and java.io.ObjectOutputStream are used to perform read and write of objects. Object read/write involves serialization/de-serialization of data, hence it involves special handling. (Most of the classes under object streams support serialization/de-serialization of data). There is complex logic involved in reading and writing of objects. Reason behind complexity are – objects have type hence regeneration should also result in type match with original object. Second is object have hierarchies. These hierarchies should also be satisfied in object streams transfer.

Summary:

java.io package provides many classes to interact with files on operating systems. Usually JVM interfaces with operating system, in other words, it shields program from the complexities of operating system interactions. But File handling is the area where the program has to directly deal with underlying operating system. java.io package offers many classes which serve general as well as specific purposes in file dealing. By selecting correct class, we can write better performing applications.


Monday, 11 April 2011

Calculate Free Disk Space in Java using Apache Commons IO

As a Java developer, lot of times I have to play around with file system. Sometimes I have to copy files/directories from one location to another; sometimes have to process certain files depending on certain pattern. In one of my test program, I wanted to calculate available disk space using Java. Lot of code snippets are available for this task. I liked the one using Apache Commons IO library.
Here is a simple trick for Java developers to calculate free diskspace. We have used Apache Commons IO library to calculate this.
Apache Commons IO library contains a class org.apache.commons.io.FileSystemUtils which can be used to calculate the free disk space in any system. Let us see the Java code for this.

 
import java.io.IOException;
 
import org.apache.commons.io.FileSystemUtils;
 
public class DiskSpace {
    public static void main(String[] args) {
        try {
 
            //calculate free disk space
            double freeDiskSpace = FileSystemUtils.freeSpaceKb("C:");
 
            //convert the number into gigabyte
            double freeDiskSpaceGB = freeDiskSpace / 1024 / 1024;
 
            System.out.println("Free Disk Space (GB):" + freeDiskSpaceGB);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Free Disk Space (GB): 40.145268
In above code we used FileSystemUtils.freeSpaceKb( ) method to get the free space in kilo byte. This method invokes the command line to calculate the free disk space. You may want to call this method in following way to get free disk space in Windows and Linux.

FileSystemUtils.freeSpaceKb("C:");       // Windows
FileSystemUtils.freeSpaceKb("/volume");  // *nix
The free space is calculated via the command line. It uses ‘dir /-c’ on Windows, ‘df -kP’ on AIX/HP-UX and ‘df -k’ on other Unix.
In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class.

Sunday, 1 August 2010

Input or read in java from Standard IO

Lets first see what we mean by standard stream in java. Click here.

  1. Using scanner

    To read console input, you first construct a Scanner that is attached to the "standard input stream" System.in.
    Scanner in = new Scanner(System.in);


    Now you use the various methods of the Scanner class to read input. For example, the nextLine method reads a line of input.



    System.out.print("What is your name? ");
    String name = in.nextLine();

    Here, we use the nextLine method because the input might contain spaces. To read a single word (delimited by whitespace), call


    String firstName = in.next();


    To read an integer, use the nextInt method.

    System.out.print("How old are you? "); 
    int age = in.nextInt();

    Similarly, the nextdouble method reads the next floating-point number.

    Finally, add the line



    import java.util.*;



  2. Using JOptionPane


    String input = JOptionPane.showInputDialog(promptString)

    eg.

    String name = JOptionPane.showInputDialog("What is your name?");

    Entering Integers
    String name = JOptionPane.showInputDialog("What is your age?");
    int age = Integer.parseInt(input);


    Import
    import javax.swing.*;
     

    Finally, whenever your program calls JOptionPane.showInputDialog, you need to end it with a call to System.exit(0). The reason is a bit technical. Showing a dialog box starts a new thread of control. When themain method exits, the new thread does not automatically terminate. Toend all threads, you call the System.exit method.


  3. Using BufferedReader Its most commonly used constructor is shown here:

    BufferedReader(Reader inputReader)
    Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use the following constructor:
    InputStreamReader(InputStream inputStream)

    So basic step is :

    InputStreamReader inp = new InputStreamReader(System.in)
    BufferedReader br = new BufferedReader(inp);

    Now to read character :

    char c = br.read();

    To read String:

    String str = br.readLine();



    • Reading Characters
      int read( ) throws IOException
      Each time that read( ) is called, it reads a character from the input stream and returns it
      as an integer value. It returns –1 when the end of the stream is encountered. As you can
      see, it can throw an IOException.


      char c;
      BufferedReader br = new
      BufferedReader(new InputStreamReader(System.in));
      c = (char) br.read();
      System.out.println(c)


    • Reading strings
      String readLine( ) throws IOException


      BufferedReader br = new BufferedReader(new
      InputStreamReader(System.in));
      String str;
      str = br.readLine();

Formatting output in java