IO in java
Stream A stream is a sequence of data.In Java a stream is composed of bytes. System.out System.on System.err Character Stream Byte Stream
Character & ByteStream classes
File class includes properties of a file or directory. Ex: Create a file, read and write permission, last modified, length. The File object represents the actual file/directory on the disk.
Constructor File(File parent, String child) File(String pathname) File(String parent, String child)
Methods public String getName() public String getParent() public String getPath() public String getAbsolutePath() public boolean canRead() public boolean canWrite() public boolean isDirectory() public boolean isFile()
public long lastModified() public long length() public boolean delete() public boolean createNewFile() throws IOException public boolean exists()
PATH Separator for File in Java In Microsoft Windows platform its “\” In Unix and Linux platform its forward slash “/”. You can access file system separator in Java by system property file.separator and its also made available from File Class by public static field separator.
Character Stream FileReader FileWriter Object Reader Writer Buffered Reader InputStream Reader FileReader Writer BufferedWriter OutputStreamWriter FileWriter Print Writer Character Stream
FileWriter Constructor (throws IOException) FileWriter(String filepath) FileWriter(String filepath, boolean append) FileWriter(File obj) Methods (of superclass Writer) void close() void write(char c[]), void write (int c) void write(String s)
FileReader Constructor (may throw Exception) FileReader(String filepath) FileReader(File obj) Methods (of superclass Reader) void close() int read() int read(char c[])
Character 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, int bufsize) Methods: same as Writer void newLine() throwsIOException program
BufferedReader It buffers input from character stream. Constructor: BufferedReader(Reader r) BufferedReader (Reader r, int bufsize) Methods: same as Reader void readLine() throwsIOException program
PrintWriter It displays string equivalents of simple type such as int, float, char, etc.. Constructor: PrintWriter(OutputStream os) PrintWriter(OutputStream os,boolean flushOnNewline) PrintWriter(Writer w) PrintWriter(Writer w, os,boolean flushOnNewline)
Methods: print() println() program:
Why Use Files for I/O? Keyboard input, screen output deal with temporary data When program ends, data is gone Data in a file remains after program ends Can be used next time program runs Can be used by another program
Text Files and Binary Files All data in files stored as binary digits Long series of zeros and ones Files treated as sequence of characters called text files Java program source code is one example Can be viewed, edited with text editor All other files are called binary files Movie files, music files, Java class files Access requires specialized program
ByteStream Allows a programmer to work with binary data in a file.
Buffered Output Stream Object Input Stream FileInput Stream FilterInput Stream Buffered Input Stream Data Input Stream Output Stream FileOutputStream FilterOutput Stream Buffered Output Stream Data Output Stream PrintStream Byte Stream
FileOutputStream It allows you to write binary data to a file. If you have to write primitive values then use FileOutputStream Constructor: FileOutputStream(String filePath) throws IOException FileOutputStream(File obj) throws IOException Program
Methods (OutputStream class) void close() void write(byte c[]) All throws IOException
FileInputStream It allows you to read binary data from a file. It should be used to read byte-oriented data for example to read image, audio, video Constructor: FileInputStream(String filePath) throws FileNotFoundException FileInputStream(File obj) throws FileNotFoundException Program
Methods(of InputStream class) void close() int read() int read(byte b[])
InputStreamReader behaves as bridge from bytes stream to character stream. InputStreamReader (InputStream in) InputStreamReader (InputStream in, String encoding) void close () Int read ()
OutputStreamWriter behaves as a bridge to transfer data from character stream to byte stream OutputStreamWriter (OutputStream out) OutputStreamWriter (OutputStream out, String encoding) void close () void write (int c) void write (char [] buffer, int offset, int length)
Character Encoding scheme for representing characters JAVA - 16 bit Unicode character encoding host platform may use different encoding scheme - the encoding of the platform may be specified. UTF-8 - UniCode Transformation Format
DataOutputstream Class It allows to write simple java types to byte output stream. Constructor: DataOutputStream(OutputStream os)
Methods void writeInt(int i) Void writeBoolean(Boolean b) Void writeByte(Byte b) Void writeChar(int i) 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() boolean readBoolean char readChar() double readDouble() float readFloat() long readLong() short readShort() int readInt() string readUTF() Program
BufferedOutputStream Buffers output to a byte stream. Constructor: BufferedOutputStream(OutputStream os) BufferedOutputStream(OutputStream os, int bufSize) Program:
BufferedInputStream Buffers input from a byte stream. Constructor: BufferedInputStream(InputStream is) BufferedInputStream(InputStream is, int bufSize) 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) int skipByte(int n)