Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
I/O
10-7-2013
I/O in Java
I/O streams vs. Reader/Writer
HW#3 due today
Reading Assignment:
Java tutorial on Basic I/O
public class Swimmer implements Cloneable {
public Date getEventDate() {
return (Date) eventDate.clone();
// note: Date must be cloneable
}
}
Exercise: rewrite the above to use a copy
constructor rather than clone to solve the
problem
key ideas:
Separate the classes that read and write
bytes from the classes that interpret and
format the data
Layering: start with a stream & wrap other
classes around it to add functionality
consequence - networking is facilitated
package java.io (also see java.nio)
text:
Reader, Writer
binary: InputStream, OutputStream
converting from stream to text: InputStreamReader
buffering: BufferedReader
package java.util
Scanner
InputStream
OutputStream
byte-oriented I/O (8-bit)
binary data
machine-readable only
Reader
Writer
char-oriented I/O (16-bit)
text data
human-readable
RandomAccess binary
key idea: layered object
start with a stream & wrap other classes
around it to add functionality
standard input, standard output & standard
error (default to console, but can be redirected
to any stream)
defined in the class System (which is in
java.lang)
public static final InputStream in;
public static final PrintStream out;
public static final PrintStream err
try {
int b = System.in.read();
} catch (IOException e) …
OK for reading in a single byte of raw data, but
what about numbers (int, float, …) or a line of text?
Would like to use readLine(), but to do this you
need a buffered reader, not a Stream like System.in
use InputStreamReader to convert from a Stream
to a Reader
then can use the method readLine() in
BufferedReader
public String readLine() throws IOException
Reads a line of text. A line is considered to be
terminated by any one of a line feed ('\n'), a
carriage return ('\r'), or a carriage return followed
immediately by a linefeed.
Returns:
A String containing the contents of the line,
not including any line-termination
characters, or null if the end of the stream
has been reached
Throws:
IOException - If an I/O error occurs
constructors for InputStreamReader include:
InputStreamReader( InputStream in );
constructors for BufferedReader include:
BufferedReader( Reader rin );
constructors for Scanner include:
Scanner( InputStream source );
Scanner( File source );
Scanner( String source );
Choose one of the abstract classes
Wrap functionality around it (buffers, files,
compression, etc.)
Format, if desired
java.text
package – handles fonts, precision,
internationalism, etc
BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) )
// Read and echo lines until EOS
public static final String EOS = null;
try {
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String inLine;
while (( inLine = stdin.readLine()) != EOS)
System.out.println(inLine);
stdin.close();
} catch (IOException e) …
// Read an int from standard input
try {
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
Two potential
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine); errors
System.out.println(“Integer value is: “ + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: “ + e);
}
error message written to Standard Error
Input
InputStreamReader
Output
OutputStreamReader
BufferedReader
BufferedWriter
PrintWriter, PrintStream
FileReader
FileWriter
StringReader
StringWriter
DataInputStream
DataOutputStream
ObjectInputStream
ObjectOutputStream
ZipInputStream
GZIPInputStream
ZipOutputStream
GZIPOutputStream
System.out.println("Hello" + new Date());
System.out is a PrintStream (OutputStream
+ ability to print data)
doesn't throw Exceptions (can use method
checkError() to detect errors)
println() method is polymorphic
Object, String, int, float, char[], boolean
+ is polymorphic
a + b can mean String concatenation or
addition, depending on what a and b are
println adds the correct end-of-line characters
for the target OS
\r\n in Windows
\n in Unix
\r in Mac
System.getProperty("line.separator") returns
this string of end-of-line characters
// toString revisited
public class MyClass {
private String myName;
problem:
platform-dependent
public String toString() {
return "BadNewline@" + "\n" + myName;
}
public static void main(String[] args) {
MyClass jack = new MyClass("Jack Frost");
System.out.println(jack);
}
// toString revisited
public class BetterClass {
platform-independent
private String myName;
public static final String NEWLINE =
System.getProperty(“line.separator”);
public String toString() {
return “GoodNewline@" + NEWLINE + myName;
}
public static void main(String[] args) {
BetterClass jack = new BetterClass("Jack Frost");
System.out.println(jack);
}
BufferedReader inData =
new BufferedReader(
new InputStreamReader(System.in)
);
data source
BufferedReader inData =
new BufferedReader(
new FileReader( “myData.txt”)
);
data source
default: current
directory
BufferedReader inData =
new BufferedReader(
new FileReader( “myData.txt”));
or
File filePath = new File(“c:\\cs242\\”);
BufferedReader inData =
new BufferedReader(
new FileReader(filePath, “myData.txt”));
class File is an abstract representation of file and
directory pathnames (java.io.File, cf. p 452 HFJ2)
public FileReader(String fileName)
throws FileNotFoundException
Creates a new FileReader, given the name of the file
to read from.
Parameters:
fileName - the name of the file to read from
Throws:
FileNotFoundException - if the named file does not
exist, is a directory rather than a regular file, or for
some other reason cannot be opened for reading.
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“myData.txt”));
Read an
integer from
the file
myData.txt
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“myData.txt”));
Read an
integer from
the file
myData.txt
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: “ + val);
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“myData.txt”));
Read an
integer from
the file
myData.txt
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: “ + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“myData.txt”));
Read an
integer from
the file
myData.txt
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: “ + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (FileNotFoundException e) {
System.err.println(“Could not open file for reading”);
Read an
integer from
the file
myData.txt
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“myData.txt”));
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: “ + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (FileNotFoundException e) {
System.err.println(“Could not open file for reading”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: “ + e);
}
// Read and echo lines, using BufferedReader
public static final String EOS = null;
try {
BufferedReader stdin =
new BufferedReader(
new FileReader(“MyData.txt”));
String inLine;
while (( inLine = stdin.readLine()) != EOS)
System.out.println(inLine);
stdin.close();
} catch (IOException e) {…
Java 1.5 introduced a new class: Scanner
that makes it easy to read and parse strings and
primitive types using regular expressions (class
java.util.regex.Pattern)
constructors include:
Scanner( File pathname );
Scanner( InputStream is );
Scanner( String aLine );
Use Scanner to read an
try {
int from standard input
Scanner scan =
new Scanner( System.in );
int val = scan.nextInt();
System.out.println(“Integer value is: ” + val);
} catch (InputMismatchException e) {
System.err.println(“Did not input an int”);
} catch …
// what other exceptions?
Use Scanner to read an
try {
int from a text file
Scanner scan =
new Scanner( new File(“mydata.txt”) );
int val = scan.nextInt();
System.out.println(“Integer value is: “ + val);
} catch (InputMismatchException e) {
System.err.println(“Did not input an int”);
} catch (FileNotFoundException e) {
System.err.println(“Couldn’t open file for reading”);
} catch …
// what other exceptions?
Read an
try {
integer from
BufferedReader stdin =
the file
new BufferedReader(
myData.txt
new FileReader(“sample.txt”));
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: “ + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (FileNotFoundException e) {
System.err.println(“Could not open file for reading”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: “ + e);
}
try {
Scanner scan =
new Scanner(
new File(“sample.txt”));
Java 1.5
Scanner class
int val = scan.nextInt();
System.out.println(“Integer value is: “ + val);
} catch (InputMismatchException e) {
System.err.println(“Next token is not an int”);
} catch (FileNotFoundException e) {
System.err.println(“Could not open file for
reading”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: “ + e);
}
try {
Use BufferedReader to
read an int from
standard input
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: ” + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: ” + e);
}
try {
Use BufferedReader to
read an int from a text
file
BufferedReader stdin =
new BufferedReader(
new FileReader(“mydata.txt”) );
String inLine = stdin.readLine();
int val = Integer.parseInt(inLine);
System.out.println(“Integer value is: ” + val);
} catch (NumberFormatException e) {
System.err.println(inLine + “ is not an int”);
} catch (FileNotFoundException e) {
System.err.println(“Couldn’t open file for reading”);
} catch (IOException e) {
System.err.println(“Unexpected IO error: ” + e); }
consider the text “1342”
Unicode: 00 31 00 33 00 34 00 32
most environments use their own char
encoding; e.g. windows: 31 33 34 32
Java provides conversion filters from
Unicode to the char encoding used by the
local OS
InputStreamReader converts an input stream
containing bytes in a particular char encoding
into a reader that emits Unicode chars
public
InputStreamReader(InputStream in,
CharsetDecoder dec)
OutputStreamReader is similar
The char data type is based on the original
Unicode specification, which defined characters
as fixed-width 16-bit entities. The Unicode
Standard has since been changed to allow for
characters whose representation requires more
than 16 bits.
For more information on Unicode 6.2.0, see
www.unicode.org.
e.g. read keystrokes from console (recall that
System.in is an InputStream), and convert to
Unicode:
‘A’..’Z’ \u0041 .. \u005a
‘a’..’z’ \u0061 .. \u007a
‘0’..’9’ \u0030 .. \u0039
InputStreamReader isr =
new InputStreamReader(System.in);
int x = isr.read();
user types 4
what is x (in decimal)?
52 (hex 34)
See www.unicode.org/charts/
8859_1
8859_5
ISO Latin-1 (default)
ISO Latin/Cyrillic
Big4
Traditional Chinese
etc.
So, to open a file using one of these, you would call
new InputStreamReader(
new FileInputStream(“mydata”), “8859_5” )
Cyrillic