input output
play

Input/Output Cmd Line Input Formatted I/O Formatted Output - PowerPoint PPT Presentation

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


  1. 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

  2. SSC1: I/O Topic Overview 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 Memory Mapping 3. Advanced Techniques: buffering, file system File System manipulations, memory mapping, polling, Manipulations . . . java.nio.* Polling

  3. SSC1: I/O Simple Input/Output Techniques Volker Sorge Overview I/O Streams Basic Streams Data Streams Standard Streams I/O Redirection ◮ Streams: the basis of all data input/output Cmd Line Input ◮ Redirecting Standard Input/Output: turning Formatted I/O Formatted Output System.in, System.out into streams Formatted Input Buffered Streams ◮ Console: some specialities dealing with direct input and Memory Mapping passwords File System Manipulations Polling

  4. SSC1: I/O What is a stream Volker Sorge Overview I/O Streams Basic Streams ◮ Streams are an abstraction over the actual low-level Data Streams Standard Streams operations of data handling. I/O Redirection ◮ 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 ◮ A stream can be viewed as a sequence of data elements Memory Mapping ◮ A stream represent either a data source (input) or data File System Manipulations sink (output) Polling ◮ There are different type of streams depending on what data s communicated and how.

  5. SSC1: I/O Types of streams Volker Sorge Overview I/O Streams There are streams of various types. We will have a look at Basic Streams Data Streams Byte Streams The most basic of all streams, they handle Standard Streams single bytes. I/O Redirection Cmd Line Input Character Streams Handle input/output of characters. Formatted I/O Data Streams Handle primitive data types and Strings. Formatted Output Formatted Input Object Streams Handle all kinds of objects. Buffered Streams Memory Mapping Obviously each type of streams can be sub-divided into two 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.

  6. SSC1: I/O Basic operations on Input streams 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.

  7. SSC1: I/O Basic operations on Output streams 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 ◮ Flushing can be done more than once. It is Buffered Streams recommended to flush at all critical points in a program. Memory Mapping ◮ Closing an output stream will automatically flush all File System Manipulations output. Polling ◮ Not closing a stream does not cause an error. Too many open streams do!

  8. SSC1: I/O Byte Streams Volker Sorge Overview I/O Streams Basic Streams Data Streams ◮ Byte Streams are implemented in the InputStream and Standard Streams I/O Redirection OutputStream classes in the java.io package. Cmd Line Input ◮ They represent the most basic stream class in Java and Formatted I/O Formatted Output can be used for various purposes. Formatted Input ◮ A mentioned before we will concentrate on file Buffered Streams Memory Mapping input/output which is implemented in File System FileInputStream and FileOutputStream Manipulations ◮ They enable to read and write ASCII files byte-by-byte. Polling

  9. SSC1: I/O A simple example program Volker Sorge // A simple Byte stream example. // Call with: // java io1 infile outfile Overview // Copies infile to outfile and prints the intermediate bytes. I/O Streams // Source: Sun Java Tutorials Basic Streams import java . io . ∗ ; Data Streams Standard Streams public class io1 { I/O Redirection public static void main ( String [] args ) throws IOException { Cmd Line Input FileInputStream in = null ; Formatted I/O FileOutputStream out = null ; Formatted Output try { Formatted Input in = new FileInputStream ( args [0]); Buffered Streams out = new FileOutputStream ( args [1]); int c ; Memory Mapping while (( c = in . read ()) != − 1) { File System System . out . println ( c ); Manipulations out . write ( c ); } Polling } finally { if ( in != null ) { in . close (); } if ( out != null ) { out . close (); } } } }

  10. SSC1: I/O A simple example program Volker Sorge Let’s take a closer look at the program: Overview 1. io1 takes two command line arguments: I/O Streams Basic Streams ◮ the input file ( args[0] ), which should exist; Data Streams ◮ the output file ( args[1] ), which will be created or Standard Streams I/O Redirection overwritten. Cmd Line Input 2. It reads bytes of input until it reaches the end of file. Formatted I/O This is checked by testing for -1 . Formatted Output 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.

  11. SSC1: I/O Character Streams Volker Sorge Overview I/O Streams Basic Streams Data Streams Standard Streams ◮ 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 ◮ Their constructors are Formatted Output Formatted Input ◮ FileReader instead of FileInputStream , and Buffered Streams ◮ FileWriter instead of FileOutputStream . Memory Mapping ◮ They also enable us to read and write ASCII files File System byte-by-byte. Manipulations Polling

  12. SSC1: I/O A simple Character Stream IO class Volker Sorge Overview public class io3 { I/O Streams public static List < Integer > read_from_file ( String filename ) throws IOException Basic Streams { Data Streams FileReader in = new FileReader ( filename ); 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) { Formatted I/O inputList . add ( input ); } Formatted Output in . close (); Formatted Input return ( inputList ); 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.

  13. SSC1: I/O Data Streams Volker Sorge ◮ Data Streams support I/O of primitive data type values: Overview boolean, char, byte, short, int, long, float, and double I/O Streams ◮ In addition they work on String values. Basic Streams Data Streams ◮ Data streams read and write binary files. Standard Streams I/O Redirection ◮ The constructors are wrappers for byte streams. E.g.: Cmd Line Input DataInputStream in = new DataInputStream ( new FileInputStream ( filename )); Formatted I/O Formatted Output DataOutputStream out = new DataOutputStream ( new FileOutputStream ( filename )); Formatted Input Buffered Streams Memory Mapping File System Manipulations Polling

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend