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

Thursday, 19 January 2012

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 :

Tuesday, 17 May 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:
/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, 
length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 
offset, length);
int ch;

while ((ch = bis.read()) != -1)
{
System.out.print((char)ch);
}

}



Using the above function:

String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

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.

Thursday, 12 May 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:

/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, offset, length);
int ch;

while ((ch = bis.read()) != -1)
{
System.out.print((char)ch);
}

}




Using the above function:



String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

Read byte array using ByteArrayInputStream Example

Here we read the byte array and put it on console.

Function to read the byte array:

public static void readByteArray(byte[] bytes){
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);

int ch;

//read bytes from ByteArrayInputStream using read method
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
}


}



Using the above function:



String str = "ByteArrayInputStream Example!";

//get bytes from string using getBytes method
byte[] bytes = str.getBytes();
readByteArray(bytes);



For partial reading of byte array read here.

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?

Monday, 2 May 2011

How to write Java code to listen runtime changes in text file

I had a requirement to write a java code to listen continues changing text file.I have used following code to do that.

public void listenServer() throws IOException {
Reader fileReader = new FileReader("/home/vaani/server.log");
BufferedReader input = new BufferedReader(fileReader);
String line = null;
while (true) {
if ((line = input.readLine()) != null) {
System.out.println(line);
continue;
}
try {
Thread.sleep(1000L);
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
break;
}
}
input.close();
return isException;
}

Monday, 25 April 2011

Turn String into InputStream

import java.io.*;

public InputStream stringToStream(String s) {
return new ByteArrayInputStream(s.getBytes());
}

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.