1 / 29

Java I/O Streams

Java I/O Streams. Stream. A stream is an abstract representation of an input and output device used to read and write data. Advantages of Streams. No need to learn inner working of each device separately. The program will work for different input and output devices without any code changes.

bisa
Download Presentation

Java I/O Streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java I/O Streams

  2. Stream • A stream is an abstract representation of an input and output device used to read and write data.

  3. Advantages of Streams • No need to learn inner working of each device separately. • The program will work for different input and output devices without any code changes.

  4. Types of Stream • Byte Stream • Character Stream

  5. Types of Stream • InputStream and OutputStream are designed for byte streams. • Reader and Writer are designed for character streams. • Use the character stream classes when working with characters or strings, and • Use the byte stream classes when working with bytes or other binary objects.

  6. Streams • JAVA distinguishes between 2 types of streams: • Text – streams, containing ‘characters‘ Program I ‘ M A S T R I N G \n Device • Binary Streams, containing 8 – bit information Program 01101001 11101101 00000000 Device

  7. Input and Output Streams • Output Streams • When writing data in a stream • E.g a file, disk etc • Input Stream • When read data from a stream • E.g disk file, the keyboard, or a remote computer

  8. Buffered Stream • Transferring data to or from a stream is extremely inefficient. • The solution is a BufferedStream. • A buffer is simply a block of memory that is used to batch up the data that is transferred to or from an external device.

  9. Byte Streams

  10. Byte Streams • When you write data to a binary stream, the data is written to the stream as a series of bytes. • Binary numerical values are just written as a series of bytes • E.g 4 bytes for int , 8 bytes for long etc. • Characters are stored internally as Unicode characters. • Each is written in binary stream as 2 bytes

  11. Sub Classes of Input Stream Class

  12. Output Stream Class • The OutputStream class contains three write() methods for writing binary data to the stream. • Mirror the read() methods of the InputStream class

  13. SubClasses of OutputStream

  14. Character Streams

  15. Character Streams Character streams are used for storing and retrieving text. All binary numeric data has to be converted to a textual representation before being written to a character stream.

  16. Stream Readers and Writers • Stream Readers and Writers are objects that can read and write byte streams as character streams. • Reader • Writer

  17. Reader • Implements Readable interface • Methods • int read() • int read(char[] cbuf)

  18. Methods of Writer

  19. Using Reader

  20. Using BufferReader • BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))

  21. Using Writers

  22. “File Handling”

  23. Topics to Cover • File-Reading • Writing to files • Example programs on file handling

  24. File-Reading • Commands for opening the file for reading: • FileReader fr = new FileReader(“input.txt”);BufferedReader br = new BufferedReader(fr); • Command for reading one line from the opened file: • String str = br.readLine(); • Command for closing the opened file: • br.close();

  25. Example import java.io.*; public class FileR { public static void main(String [] args) throws Throwable { String str; FileReader fr = new FileReader("input.txt"); BufferedReader br = new BufferedReader(fr); str= br.readLine(); System.out.println(str); } br.close(); } }

  26. File-Writing • Commands for opening the file for writing: • FileWriter fw = new FileWriter(“output.txt”);BufferedWriter bw = new BufferedWriter(fr); • Command for writing a String to the opened file: • bw.write(str); // str is a String • Command for closing the opened file: • bw.close();

  27. Example import java.io.*; import java.util.*; public class FileW { public static void main(String [] args) throws Throwable { String str; FileWriter fr = new FileWriter(“output.txt"); BufferedWriter br = new BufferedWriter(fr); Scanner c=new Scanner(System.in); br.write(c.next()); br.close(); } }

  28. LAB TASK # 1 • Open input file for reading • Open output file for writing • Copy the contents of input file in output file • Close all opened files nlp-ai@cse.iitb

  29. LAB TASK # 2 • Write a program to search a text file and print only those lines which has the search word in it. • Take i/p word from user • Open the text file • Read line by line from it • Search for i/p word in each line • Close file at the end

More Related