1 / 20

1 2 : The Java I/O System

1 2 : The Java I/O System. stream model. Byte Streams. Byte Streams : These streams are typically used to read and write binary data such as images and sounds. Byte Streams. Byte Streams : These streams are typically used to read and write binary data such as images and sounds.

minda
Download Presentation

1 2 : The Java I/O System

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. 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.*; publicclass IOStreamDemo { // Throw exceptions to console: publicstaticvoid 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.*; publicclass Echo { publicstaticvoid 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.*; publicclass ChangeSystemOut { privatestatic Test monitor = new Test(); publicstaticvoid 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.*; publicclass Redirecting { // Throw exceptions to console:publicstaticvoid 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(3.14159); out.writeUTF("That was pi"); out.writeDouble(1.41413); 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()); System.out.println(in.readDouble()); 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.

More Related