Java I/O

This describes how Java performs Input/Output operations to both files and Standard Input, Standard Output, and Standard Error. You can only do file I/O with applications NOT applets becauses it would be a security violation. Many network violations happen because languages allow writting or reading from files on the system.

All of the I/O related classes is located in the java.io package. You should import this package at the top to use the classes.

Standard Stream Classes


(part of java.lang)
Standard Input Stream System.in
(of class InputStream)
  • System.in.read() reads next byte of data from stdin.
  • System.in.skip(long x) skips over and discards x bytes of data from stdin.
  • System.in.read(byte b[]) reads up to b.length bytes of data from stdin into the array b.
Standard Output Stream System.out
(of class PrintStream)
  • System.out.print(int) prints integer to stdout.
  • System.out.print(String) prints string to stdout.
  • System.out.println(String) prints string to stdout and returns line.
Standard Error Stream System.err
(of class PrintStream..will go to screen or special destination specified by host)

Exercise 1





Basic Input and Output Stream Classes


(part of java.io)
public abstract class InputStream
  • reads a stream of BYTES.
  • Is the parent of all input stream classes.
  • IS.read() reads next byte of input from stream.
public abstract class Reader
  • like InputStream but reads a stream of CHARACTERS!!!!
  • Is the parent of all reader classes.
  • R.read() reads next single character from stream.
public abstract class OutputStream
  • writes a stream of BYTES!!!!
  • Is the parent of all OutputStream classes.
  • OS.write(byte b[]) writes b.length bytes from the b[] array.
  • OS.close() closes stream
public abstract class Writer
  • like OutputStream but reads a stream of CHARACTERS!!!!
  • Is the parent of all Writer classes.
  • W.write(char c[]) writes c.length bytes from the c[] array.
  • W.write(Strint S) writes the string S.

Derivative Input Stream Classes








Opening/Closing Reading/Writing Files


(part of java.io)
  1. OPENING (for reading): FileInputStream
        File file_handle = new File("Lynne.txt");
        FileInputStream file_input = new FileInputStream(file_handle);
    
        OR
        FileInputStream file_input = new FileInputStream("Lynne.txt);
    
  2. OPENING (for reading): FileReader
        File file_handle = new File("Lynne.txt");
        FileReader file_input = new FileReader(file_handle);
    
        OR
        FileReader file_input = new FileReader("Lynne.txt);
    
  3. OPENING (for writing): FileOutputStream
        File file_handle = new File("Lynne.txt");
        FileOutputStream file_input = new FileOutputStream(file_handle);
    
        OR
        FileOutputStream file_input = new FileOutputStream("Lynne.txt);
    
  4. OPENING (for writing): FileWriter
        File file_handle = new File("Lynne.txt");
        FileWriter file_input = new FileWriter(file_handle);
    
        OR
        FileWriter file_input = new FileWriter("Lynne.txt);
    
  5. CLOSING: for any of the above classes
        Stream.close();
    
  6. READING:DataInputStream is nice to use
    • To convert any InputStream to a DataInputStream:
          DataInputStream DIS = new DataInputStream( InputStream);
      

      For example to convert standard input.
          DataInputStream DIS = new DataInputStream(System.in);
      
    • DIS.readFloat() reads float from input stream
    • DIS.readInt() reads int from input stream
  7. READING:BufferedReader is nice to use
    • Nice because will read in a line at a time from the input.
    • To convert any Reader to a BufferedReader:
          BufferedReader BR = new BufferedReader( Reader);
      
    • BR.readLine() reads line from input and returns a String on which you can do further processing.
  8. WRITTING: DataOutputStream or BufferedWriter are nice to use (see example below)

Reading File Exercise

Writing File Exercise

Applet I/O



More exercises