1.66k likes | 3.58k Views
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.
E N D
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.
Types of Stream • Byte Stream • Character Stream
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.
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
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
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.
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
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
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.
Stream Readers and Writers • Stream Readers and Writers are objects that can read and write byte streams as character streams. • Reader • Writer
Reader • Implements Readable interface • Methods • int read() • int read(char[] cbuf)
Using BufferReader • BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))
Topics to Cover • File-Reading • Writing to files • Example programs on file handling
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();
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(); } }
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();
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(); } }
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
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