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

input output
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Software Systems Components 1

Input/Output

Volker Sorge

http://www.cs.bham.ac.uk/~vxs/teaching/ssc1

slide-2
SLIDE 2

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Topic Overview

  • 1. Simple Input/Output: streams for various types,
  • redirection. . . java.io.*
  • 2. Formatted Input/Output: dealing with mixed input,

formatting output. . . java.util.Scanner, java.util.Formatter

  • 3. Advanced Techniques: buffering, file system

manipulations, memory mapping, polling, . . . java.nio.*

slide-3
SLIDE 3

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Simple Input/Output Techniques

◮ Streams: the basis of all data input/output ◮ Redirecting Standard Input/Output: turning

System.in, System.out into streams

◮ Console: some specialities dealing with direct input and

passwords

slide-4
SLIDE 4

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

What is a stream

◮ Streams are an abstraction over the actual low-level

  • perations of data handling.

◮ Streams enable a programming language to implement

a uniform interface to the communication infrastructure

  • f diverse devices (files, sockets, hardware devices, etc.)

◮ A stream can be viewed as a sequence of data elements ◮ A stream represent either a data source (input) or data

sink (output)

◮ There are different type of streams depending on what

data s communicated and how.

slide-5
SLIDE 5

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Types of streams

There are streams of various types. We will have a look at Byte Streams The most basic of all streams, they handle single bytes. Character Streams Handle input/output of characters. Data Streams Handle primitive data types and Strings. Object Streams Handle all kinds of objects. Obviously each type of streams can be sub-divided into two basic directions: Input Stream A source that makes data available over time. Output Stream A sink into which data can be written/sent

  • ver time.
slide-6
SLIDE 6

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Basic operations on Input streams

read() Reads data (e.g. a byte) from a stream. close() Closes the stream. Although streams are generally of a sequential nature, there are some streams that support revisiting their data. mark(int n) Marks a position on the stream. n specifies “how long” this position will be remembered. reset() Resets the stream to the position where mark was last called.

slide-7
SLIDE 7

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Basic operations on Output streams

write(x) Writes some x to the stream. flush() Forces all output to be actually written. close() Closes the stream. Never forget to flush and close streams.

◮ Flushing can be done more than once. It is

recommended to flush at all critical points in a program.

◮ Closing an output stream will automatically flush all

  • utput.

◮ Not closing a stream does not cause an error. Too

many open streams do!

slide-8
SLIDE 8

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Byte Streams

◮ Byte Streams are implemented in the InputStream and

OutputStream classes in the java.io package.

◮ They represent the most basic stream class in Java and

can be used for various purposes.

◮ A mentioned before we will concentrate on file

input/output which is implemented in FileInputStream and FileOutputStream

◮ They enable to read and write ASCII files byte-by-byte.

slide-9
SLIDE 9

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

A simple example program

// A simple Byte stream example. // Call with: // java io1 infile

  • utfile

// Copies infile to outfile and prints the intermediate bytes. // Source: Sun Java Tutorials import java.io.∗; public class io1 { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(args[0]);

  • ut = new FileOutputStream(args[1]);

int c; while ((c = in.read()) != −1) { System.out.println(c);

  • ut.write(c);

} } finally { if (in != null) { in.close(); } if (out != null) {

  • ut.close();

} } } }

slide-10
SLIDE 10

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

A simple example program

Let’s take a closer look at the program:

  • 1. io1 takes two command line arguments:

◮ the input file (args[0]), which should exist; ◮ the output file (args[1]), which will be created or

  • verwritten.
  • 2. It reads bytes of input until it reaches the end of file.

This is checked by testing for -1.

  • 3. Byte by byte is read, printed out, and written to the
  • utput file.
  • 4. Finally we close both output and input stream (if they

had been opened). This automatically flushes the

  • utput stream.

Observe that we have to explicitly state that there could be an IOException in the function, otherwise the Java compiler gets confused.

slide-11
SLIDE 11

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Character Streams

◮ Character Streams are very similar to Byte Streams,

however work on 16 bit characters instead of 8 bit bytes.

◮ Their constructors are

◮ FileReader instead of FileInputStream, and ◮ FileWriter instead of FileOutputStream.

◮ They also enable us to read and write ASCII files

byte-by-byte.

slide-12
SLIDE 12

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

A simple Character Stream IO class

public class io3 { public static List<Integer> read_from_file(String filename) throws IOException { FileReader in = new FileReader(filename); List<Integer> inputList = new LinkedList<Integer>(); Integer input; System.out.println("Reading file..."); while ((input = in.read()) != −1) { inputList.add(input); } in.close(); return(inputList); } ...

The full listing is in the handouts.

The program performs the following tasks:

  • 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.
slide-13
SLIDE 13

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Data Streams

◮ Data Streams support I/O of primitive data type values:

boolean, char, byte, short, int, long, float, and double

◮ In addition they work on String values. ◮ Data streams read and write binary files. ◮ The constructors are wrappers for byte streams. E.g.: DataInputStream in = new DataInputStream(new FileInputStream(filename)); DataOutputStream out = new DataOutputStream(new FileOutputStream(filename));

slide-14
SLIDE 14

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Data Streams

◮ Data Streams support I/O of primitive data type values:

boolean, char, byte, short, int, long, float, and double

◮ In addition they work on String values. ◮ Data streams read and write binary files. ◮ The constructors are wrappers for byte streams. ◮ 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)

slide-15
SLIDE 15

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Using Data Streams

public class io4 { public static List<Integer> read_from_file(String filename) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream(filename)); List<Integer> inputList = new LinkedList<Integer>(); System.out.println("Reading file..."); try { while (true) { inputList.add(in.readInt()); } } catch(EOFException e) { in.close(); } return(inputList); } public static void write_to_file(String filename, List<Integer> outputList) throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream(filename)); ListIterator<Integer> iter = outputList.listIterator(); System.out.println("Writing file..."); while(iter.hasNext()) {

  • ut.writeInt(iter.next());
  • ut.flush();

}

  • ut.close();

} ...

We detect the end of file by catching EOFException, in- stead of testing for an invalid return value. We have to use the read/write methods for integers.

slide-16
SLIDE 16

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Object Streams

Object streams are very similar to ordinary data streams. While the latter support I/O of primitive data types, the former support I/O of objects.

◮ The constructors

ObjectInputStream/ObjectOutputStream are wrappers for byte streams.

◮ Object streams read and write binary files. ◮ Reading and writing is done with

readObject()/writeObject(obj) methods.

◮ You can mix different objects on the same stream.

slide-17
SLIDE 17

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Object Streams

Object streams are very similar to ordinary data streams. While the latter support I/O of primitive data types, the former support I/O of objects. There are some peculiarities to observe:

  • 1. Objects can have many components and therefore need

to be “serializable”: They need to have an implementation of the Serializable interface.

  • 2. Many library classes have a Serializable interface:

BigDecimals, LinkeLists, Hashtables, ArrayLists, etc.

  • 3. readObject() needs to be type cast to the correct

class, otherwise a ClassNotFoundException is

  • thrown. E.g.: BigDecimal obj1 = (BigDecimal) in.readObject();
  • 4. You can input and output primitive data types using

readObject()/writeObject(obj).

slide-18
SLIDE 18

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Data streams raise a couple of question:

  • 1. How do we get binary files suitable for data streams in

the first place?

  • 2. How can we transform ASCII files containing data

suitable for simple data types into binary format?

  • 3. If a file contains a mix of types or objects, how can we

check ahead what is read and avoid exceptions? We will answer the third question later when we discuss formatted input/output. For now we concentrate on question one and two.

slide-19
SLIDE 19

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Three Standard Streams

Standard Input (stdin) The input from the console. Its corresponding Java object is System.in Standard Output (stdout) Printing output to the console. Its corresponding Java object is System.out Standard Error (stderr) Print errors messages to the console. Its corresponding Java object is System.err Both stdout and stderr print to the console. However, separating them enables us to separate intended output from unintended error messages.

slide-20
SLIDE 20

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Example with System.in

The following program fragment turns System.in into an input stream:

import java.util.∗; import java.io.∗; public class makeIntFile { public static List<Integer> read_from_stdin() { Scanner in = new Scanner(System.in); List<Integer> inputList = new LinkedList<Integer>(); System.out.println("Reading integers..."); try { while (true) { inputList.add(in.nextInt()); } } finally { return(inputList); } } ...

The loop reads integers from the console until a non-integer element occurs. It then quits with an exception.

slide-21
SLIDE 21

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Redirecting stdin

◮ makeIntFile reads integers from stdin. ◮ Suppose it writes those to a binary file using a data

  • stream. E.g. the command line

java makeIntFile outfile

writes all integer input to outfile.

◮ We now exploit this class to translate an ASCII file

infile containing integers into its corresponding binary data file using the command

java makeIntFile outfile < infile ◮ This binds System.in to the infile as a stream. It

essentially parses integers from the file using whitespace as tokens.

slide-22
SLIDE 22

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Redirecting stdout

◮ Similar to stdin we can redirect stdout java makeIntFile outfile > outfile2 ◮ This command will

◮ read integers from the console, ◮ write them to the file outfile, ◮ saves everything that would be printed to the console to

  • utfile2

◮ E.g., everything that is printed using

System.out.println ends up in outfile2

◮ Observe that outfile and outfile2 will be different.

In particular the former is a binary file, while the latter is ASCII.

slide-23
SLIDE 23

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Redirecting both stdin and stdout

We can now simply combine the redirection in a single shell command:

java makeIntFile outfile < infile > outfile2 ◮ No input will be taken from the console. ◮ No output will be printed to the console. ◮ Error messages will still be printed to the console, the

do not go into outfile2.

◮ One can also redirect stderr in some shells (e.g., with

2> in bash).

slide-24
SLIDE 24

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Command Line Input

Finally we have a look at two special functions to allow for direct console input of regular arguments and passwords. System.console() constructs a console that has the functionality of the standard streams. E.g.

Console c = System.console();

readLine(String prompt) prints a prompt and reads a string from the console. E.g.

String login = c.readLine("Enter your login: ");

c.readPassword prints a prompt and reads a character array from the console, but without echoing the input. E.g.

char [] password = c.readPassword("Enter your password: ");

Observe that the character array contains the plain

  • characters. For security you should always immediately

process them and blank the array!

slide-25
SLIDE 25

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Formatted Input and Output

◮ Previously we have seen how to do I/O with essentially

homogeneous data.

◮ However, only very rarely will we be dealing with such a

situation, e.g. files containing only integers.

◮ For more sophisticated applications the ability to format

  • utput and reading it back is essential.

◮ Reading formatted input enables us to effectively parse

input.

◮ Formatting output gives us the means to generate

  • utput with respect to some patterns.
slide-26
SLIDE 26

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Formatted Input and Output

◮ Formatted Output: formatting to files with

PrintWriter and to strings with Formatter.

◮ Formatted Input: simple parsing of input using the

Scanner class.

slide-27
SLIDE 27

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

The PrintWriter Class

◮ We have seen previously how we can use System.out

as an output stream.

◮ Now we use the methods available to System.out for

  • utputting data to general streams.

◮ We will concentrate on print, println, and printf. ◮ These methods are implemented in the PrintWriter

class of the java.io package.

slide-28
SLIDE 28

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Constructors

◮ The PrintWriter class is essentially a wrapper for

  • utput streams.

◮ Consequently it inherits the main methods like

flush(),close(), etc.

◮ The standard constructor is PrintWriter(out), where

  • ut is either an output stream, a file, or a string. If it is

a string a file of that name will be created. E.g. the following creates a PrintWriter stream to the file

  • utput.txt. PrintWriter out = new PrintWriter("output.txt");
slide-29
SLIDE 29

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Methods

print(x) Prints a string representation of x. If works on primitive data types as well as objects using the toString method. If no toString method is implemented it prints general object information. println(x) Same as print(x) with an additional newline. printf(format,args) Prints formatted output according to the format string. Args is a comma separated list of arguments for the format string. Example:

  • ut.printf("Dec: %d,\nHex: %04x,\nOct: %o",3446,3446,3446);

yields

Dec: 3446 Hex: 0D76 Oct: 6566

slide-30
SLIDE 30

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Printf

Let’s have a closer look at the printf and its formatting argument:

◮ The format string is a mix of ordinary string, conversion

characters and control characters.

◮ Control characters start with a backslash. ◮ Conversion characters start with a percentage sign.

They “consume” arguments from the given list.

◮ Conversions can have flags for additional formatting

purposes.

slide-31
SLIDE 31

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Printf

For example consider the format string "Dec: %d,\nHex: %04x,\nOct: %o"

◮ It needs three arguments which should be integers. ◮ %d prints the first integer argument in decimal format. ◮ \n prints a newline. ◮ %04x prints the second argument in hexadecimal format.

It has two flags 0 and 4, which means that the number will be printed with four digits and, if necessary filled with leading zeros.

◮ %o prints the third argument as octal number.

slide-32
SLIDE 32

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Some Conversions for Printf

Here is a selected list of conversion characters for printf Conversion Argument Result s any a string similar to print c char a unicode character d int a decimal integer

  • int

an octal integer x,X int a hexadecimal integer (small or capital) f float a decimal number a,A float a hexadecimal floating-point number Many of the conversion accept several flags. In addition there are conversion characters for time and date. For a comprehensive list see the API documentation.

slide-33
SLIDE 33

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

The Formatter Class

◮ Formatter uses the same facilities as printf. ◮ It enables output both to files and strings. ◮ The Formatter class is implemented in the java.util

package.

◮ The main constructors are:

Formatter() constructs a string, Formatter(out) opens a file.

◮ In addition to the methods on stream, we have

format(string,args) which works similar to printf. toString() which returns a formatted string.

slide-34
SLIDE 34

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Formating to File

This is very similar to using printf with PrintWriter:

  • 1. Create a file output stream with

Formatter out = new Formatter(filename);

  • 2. Write formatted output to it using format, e.g.:
  • ut.format("% 4d: %#010X %#012o\n",output,output,output);
slide-35
SLIDE 35

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Formating to String

This is slightly more interesting. The basic idea is as follows:

  • 1. Create an empty string as data sink with Formatter().
  • 2. Every call to format appends the format result to the

stream.

  • 3. The toString() method returns the collected string.

public static void outputList(List<Integer> outlist) { ListIterator<Integer> iter = outlist.listIterator(); Formatter out = new Formatter(); Integer output; while(iter.hasNext()) {

  • utput = iter.next();
  • ut.format("% 4d: %#010X %#012o\n",output,output,output);

}

  • ut.flush();

System.out.print(out.toString());

  • ut.close();

}

Observe that we have to flush and close the output string!

slide-36
SLIDE 36

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

The Scanner Class

◮ Is a sophisticated way to ◮ It offers primarily two types of functionality

“Look ahead parsing” A simple mechanism to query the next token on the input stream and handle it according to its type. Regular Expression Matching A more sophisticated way

  • f handling input tokens with respect to pattern

matching. We will exclusively concentrate on the former of the two methods.

slide-37
SLIDE 37

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Constructors

The Scanner class is essentially an iterator operating on

◮ Streams, e.g. Scanner sc = new Scanner(System.in); ◮ Files, e.g. Scanner sc = new Scanner(new File("Infile")); ◮ Strings, e.g. String input = "1 fish 2 fish red fish blue fish"; Scanner sc = new Scanner(input);

All these constructors parse the given input considering all non-whitespace characters as tokens.

slide-38
SLIDE 38

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Methods

Like other iterators Scanner offers iteration control with look ahead using the two methods: hasNext() True if there is a next token. next() Returns the next token. In addition Scanner implements a number of similar methods for special data types: hasNext () True if the next token is of a particular type. Examples: hasNextInt(), hasNextFloat(),hasNextLine(),. . . next () Returns the next token of the specified type. Example: nextInt(), nextFloat(),nextLine(),. . . nextLine() returns a string containing one input line.

slide-39
SLIDE 39

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Controlling Formatted Input

The following program fragment demonstrates how Scanner can be used for formatted input:

public static List<Integer> read_from_file(String filename) throws IOException { Scanner in = new Scanner(new File(filename)); List<Integer> inputList = new LinkedList<Integer>(); System.out.println("Reading integers..."); while (in.hasNext()) { if (in.hasNextInt()) { inputList.add(in.nextInt());} else { in.next();} } return(inputList); } ...

The read from file method extracts all integers from the input file and stores them in a list.

slide-40
SLIDE 40

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

String Matching

◮ Scanner enables to directly check for certain keywords

  • n the input stream by using string matching.

◮ Example: in.hasNext("accept") ◮ This method call returns true if the next token indeed

corresponds to the string "accept".

slide-41
SLIDE 41

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Outlook: RegExp Matching

Scanner offers a number of elaborate methods to exploit regular expression matching:

◮ Search for tokens matching a given pattern in the input. ◮ Query or skip the next token if it matches a given

pattern.

◮ Changing delimiters: Use patterns other than

whitespace to delimit the tokens on the input. You will learn about regular expressions (and their syntax) in your models of computation lecture in the second term.

slide-42
SLIDE 42

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Advanced I/O Topics

In the following we will look at

◮ Buffered Streams: Make I/O more efficient. ◮ Memory Mapping: How to deal with really large files. ◮ File System Manipulations: Doing things with files and

directories.

◮ Polling: Watching out for file changes in the future.

slide-43
SLIDE 43

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Buffered Streams

◮ Using standard I/O streams for file communication is

generally inefficient.

◮ After each operation I/O handling is done by Operating

System routines.

◮ Buffered streams allow to simulate streams in memory:

◮ Input streams are pre-loaded before being accessed. ◮ Output streams are assembled in memory until explicitly

flushed.

◮ This is generally faster in particular on large files.

Observe, that while for standard streams flushing is not always necessary, for buffered streams it is absolutely critical!

slide-44
SLIDE 44

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Using Buffered Streams

Buffered streams are simply wrappers around byte or character streams and therefore extremely easy to use.

  • 1. Byte Streams: use BufferedInputStream and

BufferedOutputStream.

BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));

  • 2. Character Streams: use BufferedReader and

BufferedWriter.

BufferedReader in = new BufferedReader(new FileReader(filename));

  • 3. Data Streams and Object Streams can be wrapped

around Buffered Streams as usual:

DataInputStream in = new DataInputStream(new BufferedInputStream (new FileInputStream(filename)));

slide-45
SLIDE 45

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Memory Mapping

◮ Memory Mapping is a technique to work with files even

more efficiently.

◮ It also helps to work with very large files that could not

be buffered entirely.

◮ So far our streams have worked in a sequential manner,

i.e., we read one byte, character or datum at a time.

◮ However, for effective memory mapping we need

random access files.

slide-46
SLIDE 46

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Memory Mapped Files

◮ The basic idea is to establish a mapping between parts

  • f a file to a memory buffer.

◮ Files are divided into virtual pages. ◮ A page is then mapped into a memory buffer and

processed there.

◮ Whenever a page boundary is crossed the old page is

written and a new one loaded.

◮ Double mapping for files to different memory buffers is

generally avoided by locking.

slide-47
SLIDE 47

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

The FileChannel Class

In Java memory mapping is primarily achieved using the FileChannel class in the java.nio.FileChannel.*.

  • 1. Create a random access file with an existing (e.g.

created with FileReader):

RandomAccessFile file = new RandomAccessFile(file, mode);

Mode is a string specifying the access right, e.g. "rw" is read-write.

  • 2. Retrieve the FileChannel from file:

FileChannel channel = file.getChannel();

  • 3. Establish a memory map of a given size at a specified

file position:

MappedByteBuffer map = channel.map(mode, position, size);

Mode here is something like READ WRITE or READ ONLY.

slide-48
SLIDE 48

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Operations on MappedByteBuffers

◮ ByteBuffers are structured as byte arrays and

therefore can be accessed using array indices and put() and get() methods.

◮ Entire blocks of data can be written at once. ◮ Specialist operations of primitive data type exist, e.g.,

putInt() or getChar()

◮ In addition MappedByteBuffers offers methods

load() and force() to load a file page into memory and to force a memory page to be written to the file, respectively.

slide-49
SLIDE 49

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Possible Problems

◮ Using Memory mapping for only small changes is often

inefficient.

◮ One has to be careful when choosing file page size. ◮ If pages are too large memory mapping can be

sometimes slower than ordinary file transfer.

◮ Also when we create a new file and map buffers to

them, it is possible to leave “holes”, i.e. file pages that are empty.

slide-50
SLIDE 50

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Accessing the File System

◮ So far we were only concerned with file input/output. ◮ This always assumed that input file exists. ◮ Or that output files could indeed be written. ◮ However, sometimes it is necessary to ◮ This can be done using methods of the File class

slide-51
SLIDE 51

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Accessing the File System

◮ Observe that there is a File class both in java.io and

java.nio. We will discuss the former, but the concepts carry over to the latter.

◮ We will only discuss some of the most important

methods.

◮ For plenty more see the corresponding API

documentation, e.g. http://java.sun.com/javase/6/docs/api/java/io/File.html

slide-52
SLIDE 52

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Creating Abstract Pathnames

◮ The easiest way to create an abstract pathname is by

passing a string to the File constructor:

File file = new File(pathname); ◮ file is so far only an abstract pathname, it has not yet

been connected to the actual file system.

◮ Only once we use it to create an input/output stream

can we get problems if the abstract pathname is invalid.

◮ Until then we can use it for file system manipulations.

slide-53
SLIDE 53

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Accessor Methods

getName() Returns the name of the file or directory denoted by this abstract pathname. getPath() Converts this abstract pathname into a pathname string. getParent() Returns the pathname string of this abstract pathname’s parent.

slide-54
SLIDE 54

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Query Methods

exists() Tests whether the file or directory denoted by this abstract pathname exists. isDirectory() Tests whether the file denoted by this abstract pathname is a directory. isFile() Tests whether the file denoted by this abstract pathname is a normal file.

slide-55
SLIDE 55

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Creator Methods

createNewFile() Creates a new, empty file with the abstract pathname. delete() Deletes the file with the abstract pathname. rename(dest) Renames the file with the abstract pathname to dest string. mkdir() Make a new directory with the abstract pathname.

slide-56
SLIDE 56

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Listing Methods

list() Lists all files and directories for the abstract pathname. listFiles() Lists all files and directories with their full pathnames.

◮ In both cases the abstract pathname has to denote an

actual directory.

◮ Both methods can be used with a filename filter as

argument.

slide-57
SLIDE 57

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

A Small Example

◮ The following paths class creates a new directory in the

tmp directory and a new file within this directory.

◮ Observe that it will only create either if they do not yet

exist.

◮ In particular, nothing will be created if new exists

already as ordinary file.

import java.io.∗; public class paths { public static void main(String[] args) throws IOException { File file = new File("/tmp/new/file"); File dir = new File(file.getParent()); if (! dir.exists() && !dir.isDirectory()) { dir.mkdir(); } if (dir.isDirectory() && !file.exists() && !file.isFile()) { file.createNewFile(); } } }

slide-58
SLIDE 58

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Outlook: Asynchronous Polling

◮ Polling is a technique that continually interrogates a

peripheral device to see if it has data to transfer.

◮ Synchronous polling the leads to lengthy interruptions

in a client program

◮ Asynchronous polling allows record events on an input

device without delaying or interrupting the client program.

◮ This is particularly useful to check, for example,

whether a file has been written or whether a directory has been changed.

slide-59
SLIDE 59

SSC1: I/O Volker Sorge Overview I/O Streams

Basic Streams Data Streams

Standard Streams

I/O Redirection

Cmd Line Input Formatted I/O

Formatted Output Formatted Input

Buffered Streams Memory Mapping File System Manipulations Polling

Watch Services in Java 7

Java 7 will contain async polling in its java.nio package.

◮ The Watch Service API enables one to register a

directories with a watch service.

◮ The service registers specified types of events (e.g. file

creation).

◮ These events can then be handled in a predefined way

(e.g. update an editor buffer).

◮ For example, this allows to watch changes on a great

number of open files. As long as Java 7 is not yet released, this is subject to change of course!