Experiment No 31 and 32

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

///////////////Exp No 31 and 32///////////////

////////////////////page 151//////////////////
XII
1. Java stream classes can be categorised into two groups based on the data type one which they operate:
1. Byte streams
2. Character Streams
Classes for file operations are:
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.
FileReader Input stream that reads from a file
FileWriter Output stream that write to a file.

2. InputStream and OutputStream Classes


Stream class Description
InputStream Abstract class that describe stream input.
OutputStream Abstract class that describe stream output.
BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream An output stream that contain method for writing java
standard data type
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.

PrintStream Output Stream that contain print() and println() method

Methods

public abstract int read() Reads a byte and returns as a integer 0-255

public int read(byte[] buf, int offset, Reads and stores the bytes in buf starting at offset.
int count) Count is the maximum read.

public int read(byte[] buf) Same as previous offset=0 and length=buf.length()

public long skip(long count) Skips count bytes.

public int available() Returns the number of bytes that can be read.

public void close() Closes stream

public abstract void write(int b) Write b as bytes.

public void write(byte[] buf, int Write count bytes starting from offset in buf.
offset, int count)

public void write(byte[] buf) Same as previous offset=0 and count = buf.length()

public void close() Closes stream


3.
The Java Reader (java.io.Reader) and Java Writer class (java.io.Writer) in Java IO work much like the
InputStream and OutputStream with the exception that Reader and Writer are character based. They are
intended for reading and writing text.
Reader
The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include a BufferedReader,
PushbackReader, InputStreamReader, StringReader and several others.
Reader reader = new FileReader("c:\\data\\myfile.txt");
int data = reader.read();
while(data != -1)
{
char dataChar = (char) data;
data = reader.read();
}
Writer
The Java Writer class is the base class of all Writers in the Java IO API. Subclasses include BufferedWriter
and PrintWriter among others.
Writer writer = new FileWriter("c:\\data\\file-output.txt");
writer.write("Hello World Writer");
writer.close();
4.
Compile time error: no suitable constructor found for FileOutputStream(no arguments)
(actual and formal argument lists differ in length)

You might also like