Byte Streams Reading and Writing Files Object Oriented

Byte Streams - Reading and Writing Files Object Oriented Programming Using Java Computer Science/ Applications D. Aruna Padma MCA, M. Tech Govt. Degree College (W), Visakhapatnam Email. Id : arunapadma. b@gmail. com

Learning Objectives • • • Byte Streams Input. Stream Classes Important Methods of Input. Stream Output. Stream Classes Important Methods of Output. Stream Writing to and Reading from files

Byte Streams • Byte. Stream classes are designed to handle 8 -bit bytes. • Provides methods for creating and handling streams • Mostly used to read and write Images, audio and videos files • Streams are unidirectional • Two byte streams are provided for two directional flow Input. Stream Output. Stream Program Output. Stream

Input. Streams • Input. Stream classes are used to read data in the form of 8 -bit bytes • Input. Stream is the super class for all Input. Stream classes • Input. Stream class is an abstract class • It defines methods for performing various input operations • All methods are inherited by its subclasses • To perform Input operations, we should create objects of subclasses

Classes of Input. Streams File. Input. Stream Object Piped. Input. Stream Buffered. Input. Stream Byte. Array. Input. Stream Pushback. Input. Stream Filter. Input. Stream Line. Number. Input. Stream Object. Input. Stream Data. Input. Stream String. Buffer. Input. Stream Sequence. Input. Stream Data. Input

Input. Streams byte Any Source Input. Stream File byte Program byte Input. Stream byte byte byte byte

Reading Close Marking Functions of Input. Stream Repositi oning Skip Bytes Available bytes J A V A P R O G R A M

Important Methods of Input. Stream Read a single Byte • public abstract int read() throws IOException Reads some number of bytes • public int read(byte[] b) throws IOException Reads specified number of bytes • public int read(byte[] b, int off, int len) throws IOException

Important Methods of Input. Stream Skips or discards number of bytes • public long skip(long n) throws IOException Estimates number of bytes that can be read or skipped over • public int available() throws IOException Mark current position • public void mark(int readlimit) J A V A P R O G R A M

Important Methods of Input. Stream Reset- reposition the mark • public void reset() throws IOException Checks stream support marking • public boolean mark. Supported() Close the Stream • public void close() throws IOException J A V A P R O G R A M

Output. Streams • Output. Stream classes are used to Write data in the form of 8 -bit bytes • Output. Stream is the super class for all Output. Stream classes • Output. Stream class is an abstract class • It defines methods for performing various operations Write Flush Close • All methods are inherited by its subclasses

Classes of Output. Streams Object File. Output. Stream Piped. Output. Stream Output Stream Byte. Array. Output. Stream Buffered. Output. Stream Filter. Output. Stream Print. Stream Object. Output. Stream Data. Output

Output. Streams Any Destination Output. Stream byte Program File Output. Stream byte

Important Methods of Output. Stream Write a single Byte • public abstract void write(byte b) throws IOException Writes some number of bytes • public void write(byte[] b) throws IOException Writes specified number of bytes • public void write(byte[] b, int off, int len) throws IOException

Important Methods of Output. Stream Force buffered output to be written • public void flush() throws IOException Close the Stream • public void close() throws IOException

Writing to a File 1. Import File. Output. Stream 2. Create an object of File. Output. Stream and provide filename as argument 3. Get bytes 4. Write bytes to file 5. Close the file

Writing into a File import java. io. File. Output. Stream; import java. io. IOException; public class File. Out. Stream. Demo{ public static void main(String args[]){ try{ File. Output. Stream fout=new File. Output. Stream(“E: \Java. Demo\testout. txt "); String s="Welcome to java File Handling"; byte b[]=s. get. Bytes(); fout. write(b); fout. close(); System. out. println("success. . . "); }catch(IOException e) {System. out. println(e); } }}

Writing to a File b=scanner. next. Byte(); Testout 1. txt 65 66 67 68 00 System. in b File. Output. Stream 01000010 0000 01000011 01000001 (true) while(b!=0) {(false) fout. write(b); b=scanner. next. Byte( ); fout. close() } AB CD

Writing into a File import java. io. File. Output. Stream; import java. io. IOException; import java. util. Scanner; public class File. Out{ public static void main(String args[]){ try{ File. Output. Stream fout=new File. Output. Stream("E: \javademo\testout 1. txt"); Scanner scanner = new Scanner(System. in); b=scanner. next. Byte(); while(b!=0) { fout. write(b); b=scanner. next. Byte(); } fout. close(); System. out. println("success. . . "); }catch(IOException e){System. out. println(e); } }}

Reading a File 1. Import File. Input. Stream 2. Create an object of File. Input. Stream and provide filename as argument 3. Read bytes 4. Display on screen 5. Close the file

Reading from a File. Input. Stream fin=new File. Input. Stream("story. txt"); story. txt AA B C D File. Input. Stream int b=fin. read(); b System. out 0000 1111 01000010 01000001 1111 01000011 0100 (true) while(b!=-1) {(false) System. out. print((char)b); b=fin. read(); } fin. close() AB CD

Reading From a File import java. io. File. Input. Stream; import java. io. IOException; public class File. In{ public static void main(String args[]){ try{ File. Input. Stream fin=new File. Input. Stream("story. txt"); int b=fin. read(); while(b!=-1) { System. out. print((char)b); b= fin. read(); } fin. close(); System. out. println(“nsuccess. . . "); }catch(IOException e){System. out. println(e); } } }

Reading From a File import java. io. File. Input. Stream; import java. io. IOException; public class File. Buff. In{ public static void main(String args[]){ try{ File. Input. Stream fin=new File. Input. Stream("story. txt"); byte b[]=new byte[200]; while((fin. read(b, 0, 200))!=-1) { String S=new String(b); System. out. print(S); } fin. close(); System. out. println("success. . . "); }catch(IOException e){System. out. println(e); } }}

Recapitulation • Byte. Stream classes are designed to handle 8 -bit bytes. • Two byte streams are provided for two directional flow • Input. Stream classes for reading bytes • Output. Stream classes for writing bytes • Main methods of Input. Stream are Read, Finding number of bytes available, Marking, Repositioning of Marker, skip over number of bytes, Close the stream. • Main methods of Output. Stream are writing , flush and close operation • To Read / Write content into file Create an instance of Input. Stream/ Output. Stream Read bytes from file and display on Console / Read data from console and Write bytes to file Close the stream

References Web References https: //www. oercommons. org/courses/java-tutorial/view https: //nptel. ac. in/courses/106/105/106105191/ Lecture 31 : I-O Stream-I https: //docs. oracle. com/javase/8/docs/api/java/io/Output. Stream. html https: //docs. oracle. com/javase/8/docs/api/java/io/Input. Stream. html Book Reference Programming with JAVA, E Balaguruswamy Java, Object-Oriented Problem Solving, Third Edition, R. Morelli and R. Walde

D. Aruna Padma MCA, M. Tech Govt. Degree College (W), Visakhapatnam Email. Id : arunapadma. b@gmail. com
- Slides: 26