Presentation is loading. Please wait.

Presentation is loading. Please wait.

12: The Java I/O System stream model.

Similar presentations


Presentation on theme: "12: The Java I/O System stream model."— Presentation transcript:

1 12: The Java I/O System stream model

2 Byte Streams Byte Streams :These streams are typically used to read and write binary data such as images and sounds.

3 Byte Streams Byte Streams :These streams are typically used to read and write binary data such as images and sounds.

4 Types of InputStream

5 Types of FilterInputStream

6 Character Streams

7 Unicode Java I/O (From v1.1)

8 Unicode Java I/O Filters(From v1.1)

9 Understanding the I/O Superclasses
int read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length) int write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length) int read() int read(char cbuf[]) int read(char cbuf[], int offset, int length) int write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)

10 How to Use File Streams The following Copy program uses FileReader and FileWriter to copy the contents of a file named farrago.txt into a file called outagain.txt: import java.io.*; public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); }

11 Typical uses of I/O streams
import java.io.*; public class IOStreamDemo { // Throw exceptions to console: public static void main(String[] args) throws IOException { // 1. Reading input by lines: BufferedReader in = new BufferedReader( new FileReader("IOStreamDemo.java")); String s, s2 = new String(); while((s = in.readLine())!= null) s2 += s + "\n"; in.close();

12 Typical uses of I/O streams
// 1b. Reading standard input: BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a line:"); System.out.println(stdin.readLine()); // 2. Input from memory StringReader in2 = new StringReader(s2); int c; while((c = in2.read()) != -1) System.out.print((char)c);

13 Typical uses of I/O streams
// 4. File output try { BufferedReader in4 = new BufferedReader( new StringReader(s2)); PrintWriter out1 = new PrintWriter( new BufferedWriter( new FileWriter("IODemo.out"))); int lineCount = 1; while((s = in4.readLine()) != null ) out1.println(lineCount++ + ": " + s); out1.close(); } catch(EOFException e) { System.err.println("End of stream"); } }} ///:~

14 Reading from standard input
Following the standard I/O model, Java has System.in, System.out, and System.err. Throughout this book, you’ve seen how to write to standard output using System.out, which is already prewrapped as a PrintStream object. System.err is likewise a PrintStream, but System.in is a raw InputStream with no wrapping. This means that although you can use System.out and System.err right away, System.in must be wrapped before you can read from it.

15 Reading from standard input
// How to read from standard input. import java.io.*; public class Echo { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String s; while((s = in.readLine()).length() != 0) System.out.println(s); // An empty line terminates the program } } ///:~

16 Changing System.out to a PrintWriter
System.out is a PrintStream, which is an OutputStream. PrintWriter has a constructor that takes an OutputStream as an argument. Thus, if you want, you can convert System.out into a PrintWriter using that constructor //: c12:ChangeSystemOut.java // Turn System.out into a PrintWriter. import com.bruceeckel.simpletest.*; import java.io.*; public class ChangeSystemOut { private static Test monitor = new Test(); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out, true); out.println("Hello, world"); monitor.expect(new String[] { "Hello, world" }); } } ///:~

17 Redirecting standard I/O
import java.io.*; public class Redirecting { // Throw exceptions to console: public static void main(String[] args) throws IOException { PrintStream console = System.out; BufferedInputStream in = new BufferedInputStream( new FileInputStream("Redirecting.java")); PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("test.out"))); System.setIn(in); System.setOut(out); System.setErr(out); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String s; while((s = br.readLine()) != null) System.out.println(s); out.close(); // Remember this! System.setOut(console); } } ///:~

18 Storing and recovering data
//: io/StoringAndRecoveringData.java import java.io.*; public class StoringAndRecoveringData { public static void main(String[] args) throws IOException { DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("Data.txt"))); out.writeDouble( ); out.writeUTF("That was pi"); out.writeDouble( ); out.writeUTF("Square root of 2"); out.close();

19 Storing and recovering data
DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("Data.txt"))); System.out.println(in.readDouble()); // Only readUTF() will recover the // Java-UTF String properly: System.out.println(in.readUTF()); }

20 Exercises 1. Read a text file, add line number to each line then print to console by using filter class LineNumberReader.


Download ppt "12: The Java I/O System stream model."

Similar presentations


Ads by Google