200 likes | 352 Views
IO in java. Stream. Character Stream Byte Stream. Character & ByteStream classes. FileWriter FileReader. Buffered Character Stream. BufferedReader BufferedWriter By buffering, no. of reads and writes to the physical device is reduced. BufferedWriter.
E N D
Stream • Character Stream • Byte Stream
FileWriter • FileReader
Buffered Character Stream • BufferedReader • BufferedWriter By buffering, no. of reads and writes to the physical device is reduced.
BufferedWriter • It buffers output to a character stream. • Constructor: BufferedWriter(Writer w) BufferedWriter(Writer w, intbufsize) Methods: same as Writer void newLine() throwsIOException program
BufferedReader • It buffers input from character stream. • Constructor: BufferedReader(Reader r) BufferedReader (Reader r, intbufsize) Methods: same as Reader void readLine() throwsIOException program
PrintWriter • It displays string equivalents of simple type such as int, float, char, etc.. • Constructor: PrintWriter(OutputStreamos) PrintWriter(OutputStreamos,booleanflushOnNewline) PrintWriter(Writer w) PrintWriter(Writer w, os,booleanflushOnNewline)
Methods: print() println() program:
ByteStream • OutputStream and InputStream class has same methods as FileWriter and FileReader.
FileOutputStream • It allows you to write binary data to a file. • Constructor: FileOutputStream(String filePath) throws IOException FileOutputStream(File obj) throws IOException Program
FileInputStream • It allows you to read binary data from a file. • Constructor: FileInputStream(String filePath) throws FileNotFoundException FileInputStream(File obj) throws FileNotFoundException Program
DataOutputstream Class • It allows to write simple java types to byte output stream. • Constructor: DataOutputStream(OutputStreamos)
Methods • void writeInt(inti) • Void writeBoolean(Boolean b) • Void writeByte(Byte b) • Void writeChar(inti) • Void writeDouble(double d) • Void writeFloat(float f) • Void writeLong(long l) • Void writeShort(short s) • Void writeUTF(String s) • Program
DataInputStream • It allows to read simple java types from byte input stream. • Constructor: DataInputStream(InputStream is)
Methods • byte readByte() • booleanreadBoolean • char readChar() • double readDouble() • float readFloat() • long readLong() • short readShort() • intreadInt() • string readUTF() • Program
BufferedOutputStream • Buffers output to a byte stream. • Constructor: BufferedOutputStream(OutputStreamos) BufferedOutputStream(OutputStreamos, intbufSize) Program:
BufferedInputStream • Buffers input from a byte stream. • Constructor: BufferedInputStream(InputStream is) BufferedInputStream(InputStream is, intbufSize) Program:
Random Access File • Previous classes only use sequential access to read and write to a file. • This class allows to write programs that can seek to any location in a file to read & write data to that point. • Constructor: • RandomAccessFile(String filenm, String mode)
Methods • void close() • long length() • int read() • int read(byte buffer[],int index, int size) • void seek(long n) • intskipByte(int n)