Institute: Uie Department: Cse: Bachelor of Engineering (Computer Science & Engineering)
Institute: Uie Department: Cse: Bachelor of Engineering (Computer Science & Engineering)
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science &
Engineering)
2
CharArrayReader
CharArrayReader is an implementation of an input stream
that uses a character array as the source.
CharArrayReader(char array[ ])
CharArrayReader(char array[ ], int start, int numChars)
Class is java.io.CharArrayReader
Read contents of char array[] as acharacter stream
Java CharArrayReader class methods
Example
import java.io.*;
public class CharArrayReaderDemo {
public static void main(String args[]) throws IOException {
String tmp = "abcdefghijklmnopqrstuvwxyz";
int length = tmp.length();
char c[] = new char[length];
tmp.getChars(0, length, c, 0); //public void getChars(int srcBegin, int
srcEnd, // char[] dst, int dstBegin)
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 5);
int i;
System.out.println("input1
is:");
while((i = input1.read()) !
= -1) {
System.out.print((char)i);
}
System.out.println();
System.out.println("input2
is:"); input1 object is constructed using the entire
lowercase alphabet,
while((i = input2.read()) !
= -1) { input2 contains only the first five letters.
System.out.print((char)i);
}
CharArrayWriter
CharArrayWriter is an implementation of an output stream that
uses an array as the destination.
System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
char c[] = f.toCharArray();
for (int i=0; i<c.length; i++)
{
System.out.print(c[i]);
}
System.out.println("\nTo a
FileWriter()");
FileWriter f2 = new
FileWriter("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a
reset"); Example demonstrates
CharArrayWriter by reworking
f.reset(); the sample program shown
for (int i=0; i<3; i++) earlier for
f.write('X'); ByteArrayOutputStream.
System.out.println(f.toString());
Object serialization
• Classes ObjectInputStream
How to Write to an ObjectOutputStream
FileOutputStream out = new
FileOutputStream("theTime");
ObjectOutputStream s = new
ObjectOutputStream(out);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();