Professional Documents
Culture Documents
August 2012
1
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
2
Thursday, August 2, 12
I/O streams
Thursday, August 2, 12
I/O streams
A program uses an input stream to read data from a source, one item
at a time.
Thursday, August 2, 12
Byte streams
Programs use byte streams to perform input and output on 8-bit bytes.
Thursday, August 2, 12
Byte streams
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
Thursday, August 2, 12
When not to use byte streams?
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
9
Thursday, August 2, 12
Character streams
All character stream classes are descended from Reader and Writer.
10
Thursday, August 2, 12
Character streams
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("input.txt");
outputStream = new FileWriter("output.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
11
}
Thursday, August 2, 12
Line-oriented I/O
public class CopyLines {
public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream = new BufferedReader(new FileReader("input.txt"));
outputStream = new PrintWriter(new FileWriter("output.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
12
}
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
13
Thursday, August 2, 12
Buffered streams
This makes a program much less efficient since each such request
often triggers disk access, network activity or other expensive
operations.
Thursday, August 2, 12
Buffered streams
15
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
16
Thursday, August 2, 12
Scanning
17
Thursday, August 2, 12
Breaking input into tokens
A scanner uses white spaces (blanks, tabs, line terminators) to separate tokens.
public class ScanInput {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("input.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
}
Use regular expressions to specify a different separator: s.useDelimiter(",\\s*");
18
Thursday, August 2, 12
Translating individual tokens
19
Thursday, August 2, 12
Translating individual tokens
public class ScanSum {
public static void main(String[] args) throws IOException {
Scanner s = null;
double sum = 0;
try {
s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt")));
s.useLocale(Locale.US);
while (s.hasNext()) {
if (s.hasNextDouble()) {
sum += s.nextDouble();
} else { 8.5
s.next(); 32,767
}
3.14159
}
} finally {
1,000,000.1
s.close();
}
System.out.println(sum); The output string is "1032778.74159"
}
} 20
Thursday, August 2, 12
Formatting
21
Thursday, August 2, 12
Formatting
public class Root {
public static void main(String[] args) {
int i = 2;
double r = Math.sqrt(i);
System.out.format("The square root of %d is %f.%n", i, r);
}
}
The square root of 2 is 1.414214.
d formats an integer value as a decimal value.
f formats a floating point value as a decimal value.
n outputs a platform-specific line terminator.
x formats an integer as a hexadecimal value.
s formats any value as a string.
tB formats an integer as a locale-specific month name.
3.141593, +00000003.1415926536
22
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
23
Thursday, August 2, 12
I/O from the command line
A program is often run from the command line and interacts with the user in the
command line environment. Two ways:
System.in is a byte stream with no character stream features. To use the standard
input as a character stream, do as follows:
Console: a single, predefined object that has most features provided by the standard
streams. It is particularly useful for secure password entry.
24
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
25
Thursday, August 2, 12
Data streams
Data streams support binary I/O of primitive data type values (boolean, char,
byte, short, int, long, float and double) and String values.
out.writeDouble(price);
out.writeInt(unit);
out.writeUTF(desc);
26
Thursday, August 2, 12
Content
Byte streams
Character streams
Buffered streams
Data streams
Object streams
27
Thursday, August 2, 12
Object streams
Object streams support I/O of objects. Classes support serialization of their
objects if they implement the interface Serializable.
This means that all the primitive data I/O methods supported by data
streams are also implemented in object streams.
ObjectOutputStream out = null; ObjectInputStream in = null;
Thursday, August 2, 12
Exercises
29
Thursday, August 2, 12
Exercises
Creates n students (n > 4) whose data are read from the standard
input;
Loads the saved file to restore and print the students to the
standard output.
Thursday, August 2, 12