Software Systems Components 1
Total Page:16
File Type:pdf, Size:1020Kb
SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams Software Systems Components 1 Standard Streams I/O Redirection Input/Output Cmd Line Input Formatted I/O Formatted Output Formatted Input Volker Sorge Buffered Streams Memory Mapping http://www.cs.bham.ac.uk/~vxs/teaching/ssc1 File System Manipulations Polling Topic Overview SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams 1. Simple Input/Output: streams for various types, Standard Streams I/O Redirection redirection. java.io.* Cmd Line Input 2. Formatted Input/Output: dealing with mixed input, Formatted I/O Formatted Output formatting output. java.util.Scanner, Formatted Input java.util.Formatter Buffered Streams 3. Advanced Techniques: buffering, file system Memory Mapping File System manipulations, memory mapping, polling, Manipulations ... java.nio.* Polling Simple Input/Output Techniques SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams Standard Streams I/O Redirection I Streams: the basis of all data input/output Cmd Line Input Redirecting Standard Input/Output: turning Formatted I/O I Formatted Output System.in, System.out into streams Formatted Input Buffered Streams Console: some specialities dealing with direct input and I Memory Mapping passwords File System Manipulations Polling What is a stream SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams I Streams are an abstraction over the actual low-level Data Streams operations of data handling. Standard Streams I/O Redirection I Streams enable a programming language to implement Cmd Line Input a uniform interface to the communication infrastructure Formatted I/O Formatted Output of diverse devices (files, sockets, hardware devices, etc.) Formatted Input Buffered Streams I A stream can be viewed as a sequence of data elements Memory Mapping I A stream represent either a data source (input) or data File System sink (output) Manipulations Polling I There are different type of streams depending on what data s communicated and how. Types of streams SSC1: I/O Volker Sorge Overview I/O Streams There are streams of various types. We will have a look at Basic Streams Byte Streams The most basic of all streams, they handle Data Streams Standard Streams single bytes. I/O Redirection Character Streams Handle input/output of characters. Cmd Line Input Formatted I/O Data Streams Handle primitive data types and Strings. Formatted Output Formatted Input Object Streams Handle all kinds of objects. Buffered Streams Obviously each type of streams can be sub-divided into two Memory Mapping File System basic directions: Manipulations Input Stream A source that makes data available over time. Polling Output Stream A sink into which data can be written/sent over time. Basic operations on Input streams SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams read() Reads data (e.g. a byte) from a stream. Standard Streams I/O Redirection close() Closes the stream. Cmd Line Input Formatted I/O Although streams are generally of a sequential nature, there Formatted Output are some streams that support revisiting their data. Formatted Input Buffered Streams mark(int n) Marks a position on the stream. n specifies Memory Mapping \how long" this position will be remembered. File System Manipulations reset() Resets the stream to the position where mark Polling was last called. Basic operations on Output streams SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams write(x) Writes some x to the stream. Data Streams flush() Forces all output to be actually written. Standard Streams I/O Redirection close() Closes the stream. Cmd Line Input Formatted I/O Never forget to flush and close streams. Formatted Output Formatted Input I Flushing can be done more than once. It is Buffered Streams recommended to flush at all critical points in a program. Memory Mapping File System I Closing an output stream will automatically flush all Manipulations output. Polling I Not closing a stream does not cause an error. Too many open streams do! Byte Streams SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams I Byte Streams are implemented in the InputStream and Standard Streams I/O Redirection classes in the package. OutputStream java.io Cmd Line Input I They represent the most basic stream class in Java and Formatted I/O Formatted Output can be used for various purposes. Formatted Input Buffered Streams I A mentioned before we will concentrate on file input/output which is implemented in Memory Mapping File System FileInputStream and FileOutputStream Manipulations Polling I They enable to read and write ASCII files byte-by-byte. A simple example program SSC1: I/O Volker Sorge //A simple Byte stream example. // Call with: // java io1 infile outfile Overview // Copies infile to outfile and prints the intermediate bytes. // Source: Sun Java Tutorials I/O Streams Basic Streams import java.io.∗; Data Streams Standard Streams public class io1 f I/O Redirection public static void main(String[] args) throws IOException f Cmd Line Input FileInputStream in = null; Formatted I/O FileOutputStream out = null; Formatted Output try f Formatted Input in = new FileInputStream(args[0]); out = new FileOutputStream(args[1]); Buffered Streams intc ; Memory Mapping while ((c = in.read()) != −1) f File System System.out.println(c); Manipulations out.write(c); g Polling g finally f if (in != null) f in.close(); g if (out != null) f out.close(); g g g g A simple example program SSC1: I/O Volker Sorge Let's take a closer look at the program: Overview 1. io1 takes two command line arguments: I/O Streams Basic Streams I the input file (args[0]), which should exist; Data Streams Standard Streams I the output file (args[1]), which will be created or I/O Redirection overwritten. Cmd Line Input 2. It reads bytes of input until it reaches the end of file. Formatted I/O Formatted Output This is checked by testing for -1. Formatted Input 3. Byte by byte is read, printed out, and written to the Buffered Streams output file. Memory Mapping File System 4. Finally we close both output and input stream (if they Manipulations had been opened). This automatically flushes the Polling output stream. Observe that we have to explicitly state that there could be an IOException in the function, otherwise the Java compiler gets confused. Character Streams SSC1: I/O Volker Sorge Overview I/O Streams Basic Streams Data Streams Standard Streams I Character Streams are very similar to Byte Streams, I/O Redirection however work on 16 bit characters instead of 8 bit bytes. Cmd Line Input Formatted I/O I Their constructors are Formatted Output Formatted Input I FileReader instead of FileInputStream, and Buffered Streams I FileWriter instead of FileOutputStream. Memory Mapping I They also enable us to read and write ASCII files File System byte-by-byte. Manipulations Polling A simple Character Stream IO class SSC1: I/O Volker Sorge public class io3 f Overview public static List<Integer> read_from_file(String filename) throws IOException I/O Streams f Basic Streams FileReader in = new FileReader(filename); Data Streams List<Integer> inputList = new LinkedList<Integer>(); Standard Streams Integer input; I/O Redirection System.out.println("Reading file..."); Cmd Line Input while ((input = in.read()) != −1) f inputList.add(input); Formatted I/O g Formatted Output in.close(); Formatted Input return(inputList); g ... Buffered Streams Memory Mapping The full listing is in the handouts. File System The program performs the following tasks: Manipulations Polling 1. reads characters from input file, appends them to list, (observe that characters are integers!) 2. prints the list of characters to standard output, and 3. writes all characters again to the output file. I There are read/write methods for each supported type, e.g.: Type Reader method Writer method boolean readBoolean() writeBoolean(boolean v) char readChar() writeChar(int v) int readInt() writeInt(int v) double readDouble() writeDouble(double v) float readFloat() writeFloat(float v) String readLine() writeChars(String s) Data Streams SSC1: I/O Volker Sorge I Data Streams support I/O of primitive data type values: Overview boolean, char, byte, short, int, long, float, and double I/O Streams Basic Streams I In addition they work on String values. Data Streams I Data streams read and write binary files. Standard Streams I/O Redirection I The constructors are wrappers for byte streams. E.g.: Cmd Line Input DataInputStream in = new DataInputStream(new FileInputStream(filenameFormatted)); I/O Formatted Output DataOutputStream out = new DataOutputStream(new FileOutputStream(filenameFormatted Input)); Buffered Streams Memory Mapping File System Manipulations Polling Data Streams SSC1: I/O Volker Sorge I Data Streams support I/O of primitive data type values: Overview boolean, char, byte, short, int, long, float, and double I/O Streams Basic Streams I In addition they work on String values. Data Streams I Data streams read and write binary files. Standard Streams I/O Redirection I The constructors are wrappers for byte streams. Cmd Line Input There are read/write methods for each supported Formatted I/O I Formatted Output type, e.g.: Formatted Input Type Reader method Writer method Buffered Streams boolean readBoolean() writeBoolean(boolean v) Memory Mapping File System char readChar() writeChar(int v) Manipulations int readInt() writeInt(int v) Polling double readDouble() writeDouble(double v) float readFloat() writeFloat(float v) String readLine() writeChars(String s) Using Data Streams SSC1: I/O Volker Sorge public class io4 f public static List<Integer> read_from_file(String filename) throws IOException Overview f DataInputStream in = new DataInputStream(new FileInputStream(filename)); I/O Streams List<Integer> inputList = new LinkedList<Integer>(); Basic Streams Data Streams System.out.println("Reading file..."); Standard Streams try f We detect the end of file by while (true) f I/O Redirection inputList.add(in.readInt()); catching EOFException, in- Cmd Line Input g g stead of testing for an invalid Formatted I/O catch(EOFExceptione ) f Formatted Output in.close(); return value.