220 likes | 423 Views
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.
E N D
12: 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.
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)
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(); } }
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();
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);
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"); } }} ///:~
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.
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 } } ///:~
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" }); } } ///:~
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); } } ///:~
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();
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()); } }
Exercises 1. Read a text file, add line number to each line then print to console by using filter class LineNumberReader.