Thursday, 19 January 2012
Monday, 16 January 2012
Tuesday, 17 May 2011
Read partial byte array using ByteArrayInputStream Example
/* 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
- PusbackInputStream
- StreamTokenizer
- PushbackReader
- LineNumberReader
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);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
or FilterOutputStream
:
DataInputStreamandDataOutputStreamBufferedInputStreamandBufferedOutputStreamLineNumberInputStreamPushbackInputStreamPrintStream(this is an output stream)
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);You might do this so that you can use the more convenient
String input;
while ((input = dis.readLine()) != null) {
. . . // do something interesting here
}
readXXX methods, such as readLine, implemented by DataInputStream. Using the Processing 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:
BufferedReaderandBufferedWriterBufferedInputStreamandBufferedOutputStream- 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.
FilterReaderandFilterWriterFilterInputStreamandFilterOutputStream- 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.
InputStreamReaderandOutputStreamWriter- A reader and writer pair that forms the bridge between byte streams and character streams. An
InputStreamReaderreads bytes from anInputStreamand converts them to characters using either the default character-encoding or a character-encoding specified by name. Similarly, anOutputStreamWriterconverts characters to bytes using either the default character-encoding or a character-encoding specified by name and then writes those bytes to anOutputStream. You can learn the name of the default character-encoding by callingSystem.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.
ObjectInputStreamandObjectOutputStream- Used to serialize objects. See Object Serialization.
DataInputStreamandDataOutputStream- 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. LineNumberReaderLineNumberInputStream- Keeps track of line numbers while reading.
PushbackReaderPushbackInputStream- 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.
PrintWriterPrintStream- 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 givesjava.io's data sink streams.
Sink Type Character Streams Byte Streams Memory CharArrayReader,CharArrayWriterByteArrayInputStream,ByteArrayOutputStreamStringReader,StringWriterStringBufferInputStreamPipe PipedReader,PipedWriterPipedInputStream,PipedOutputStreamFile FileReader,FileWriterFileInputStream,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:
CharArrayReaderandCharArrayWriterByteArrayInputStreamandByteArrayOutputStream- 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.
FileReaderandFileWriterFileInputStreamandFileOutputStream- 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
FileReaderandFileWriterto copy the contents of one file into another.PipedReaderandPipedWriterPipedInputStreamandPipedOutputStream- 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
PipedReaderandPipedWriterin action in How to Use Pipe Streams.StringReaderandStringWriterStringBufferInputStream- Use
StringReaderto read characters from aStringas it lives in memory. UseStringWriterto write to aString.StringWritercollects the characters written to it in aStringBuffer, which can then be converted to aString.StringBufferInputStreamis similar toStringReader, except that it reads bytes from aStringBuffer.
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
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
java.io. Reader and java.io. Writer are two main classes.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. 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. 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.
Java IO : Introduction
So lets first clear what is stream?
Monday, 2 May 2011
How to write Java code to listen runtime changes in text file
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. 
