MOD III
Input / Output Streams
Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit binary input/output of data. Byte streams are used where the program needs to work with the raw binary data.
Byte streams All byte stream classes are descended from InputStream and OutputStream.InputStreamOutputStream
The java.io package contains two classes, InputStream and OutputStream, from which most of the other classes in the package are derived.
InputStream The InputStream class defines methods for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the no of bytes available for reading, and resetting the current position within the stream. An input stream is automatically opened when you create it. You can explicitly close a stream with the close method, or let it be closed implicitly when the InputStream is garbage collected.
OutputStream OutputStream defines methods for writing bytes or arrays of bytes to the stream. An output stream is automatically opened when you create it. You can explicitly close an output stream with the close method, or let it be closed implicitly when the OutputStream is garbage collected
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class stream1 { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(“c:\\mytext.txt"); out = new FileOutputStream("e:\\outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
Character Streams Character streams works with the characters. In Java characters are stored as Unicode conventions. This kind of storage makes it platform independent, program independent, language independent. Characters storage using Unicode convention makes the Java ready for internationalization and for this Java provides the Character stream I/O.
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class stream2 { public static void main(String[] args) throws IOException { FileReader iStream = null; FileWriter oStream = null; try { iStream = new FileReader("e:\\Thanks.txt"); oStream = new FileWriter("e:\\outagain.txt"); int c; while ((c = iStream.read()) != -1) { oStream.write(c); } } finally { if (iStream != null) { iStream.close(); } if (oStream != null) { oStream.close(); } } } }
Unbuffered I/O This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
Buffered I/O streams Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
A program can convert an unbuffered stream into a buffered stream using the wrapping idiom BufferedReader inputStream; inputStream = new BufferedReader(new FileReader (“c:\\mytext.txt")); BufferedWriter outputStream; outputStream = new BufferedWriter(new FileWriter (“c:\\charoutput.txt"));
Scanning and Formatting Programming I/O often involves translating to and from the neatly formatted data. Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type – By default, a scanner uses white space to separate tokens. Formatting – Stream objects that implement formatting are instances of either PrintWriter, a character stream class, or PrintStream, a byte stream class.PrintWriterPrintStream – print, println format and format formats
import java.io.*; import java.util.Scanner; public class stream3 { public static void main(String[] args) throws IOException { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("e:\\links.txt"))); while (s.hasNext()) { System.out.println(s.next()); } } finally { if (s != null) { s.close(); }
I/O from Command Line The Java platform supports three Standard Streams: Standard Input, accessed through System.in; Standard Output, accessed through System.out; and Standard Error, accessed through System.err. These objects are defined automatically and do not need to be opened. Standard Output and Standard Error are both for output; having error output separately allows the user to divert regular output to a file and still be able to read error messages
To use Standard Input as a character stream, wrap System.in in InputStreamReader. InputStreamReader cin = new InputStreamReader (System.in);
import java.io.*; public class stream5 { public static void main(String args[]) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.print("Enter a number "); String s1 = br.readLine(); int x = Integer.parseInt(s1); int x2=x*x; System.out.print("\nThe square of number is :"+x2); } }
import java.io.*; public class stream4 { public static void main(String args[]) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int marks[] = new int[3]; for(int i = 0; i < marks.length; i++) { System.out.println("Enter a whole number"); String s1 = br.readLine(); int x = Integer.parseInt(s1); marks[i] = x; } int total = 0; System.out.print("The elements are: "); for(int i = 0; i < marks.length; i++) { System.out.print(marks[i]+"\t"); total += marks[i]; } System.out.print("\nThe average is : " + (total/marks.length)); } }
import java.io.*; public class stream4 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int marks[] = new int[3]; for(int i = 0; i < marks.length; i++) { System.out.println("Enter a whole number"); String s1 = br.readLine(); int x = Integer.parseInt(s1); marks[i] = x; } int total = 0; System.out.print("The elements are: "); for(int i = 0; i < marks.length; i++) { System.out.print(marks[i]+"\t"); total += marks[i]; } System.out.print("\nThe average is : " + (total/marks.length)); } }