Presentation is loading. Please wait.

Presentation is loading. Please wait.

IO in java.

Similar presentations


Presentation on theme: "IO in java."— Presentation transcript:

1 IO in java

2 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

3 Character & ByteStream classes

4 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.

5 Constructor File(File parent, String child) File(String pathname)
File(String parent, String child)

6 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()

7 public long lastModified()
public long length() public boolean delete() public boolean createNewFile() throws IOException public boolean exists()

8 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.

9 Character Stream FileReader FileWriter Object Reader Writer
Buffered Reader InputStream Reader FileReader Writer BufferedWriter OutputStreamWriter FileWriter Print Writer Character Stream

10 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)

11 FileReader Constructor (may throw Exception)
FileReader(String filepath) FileReader(File obj) Methods (of superclass Reader) void close() int read() int read(char c[])

12 Character Stream FileWriter FileReader

13 Buffered Character Stream
BufferedReader BufferedWriter By buffering, no. of reads and writes to the physical device is reduced.

14 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

15 BufferedReader It buffers input from character stream. Constructor:
BufferedReader(Reader r) BufferedReader (Reader r, int bufsize) Methods: same as Reader void readLine() throwsIOException program

16 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)

17 Methods: print() println() program:

18 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

19 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

20 ByteStream Allows a programmer to work with binary data in a file.

21 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

22 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

23 Methods (OutputStream class)
void close() void write(byte c[]) All throws IOException

24 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

25 Methods(of InputStream class)
void close() int read() int read(byte b[])

26 InputStreamReader behaves as bridge from bytes stream to character stream. InputStreamReader (InputStream in) InputStreamReader (InputStream in, String encoding) void close () Int read ()

27 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)

28 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 UniCode Transformation Format

29 DataOutputstream Class
It allows to write simple java types to byte output stream. Constructor: DataOutputStream(OutputStream os)

30 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

31 DataInputStream It allows to read simple java types from byte input stream. Constructor: DataInputStream(InputStream is)

32 Methods byte readByte() boolean readBoolean char readChar()
double readDouble() float readFloat() long readLong() short readShort() int readInt() string readUTF() Program

33 BufferedOutputStream
Buffers output to a byte stream. Constructor: BufferedOutputStream(OutputStream os) BufferedOutputStream(OutputStream os, int bufSize) Program:

34 BufferedInputStream Buffers input from a byte stream. Constructor:
BufferedInputStream(InputStream is) BufferedInputStream(InputStream is, int bufSize) Program:

35 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)

36 Methods void close() long length() int read()
int read(byte buffer[],int index, int size) void seek(long n) int skipByte(int n)


Download ppt "IO in java."

Similar presentations


Ads by Google