Sun Educational Services
Module 11
Console I/O and File I/O
Java Programming Language
Sun Educational Services
Objectives
Read data from the console
Write data to the console
Describe files and file I/O
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 2 of 17
Sun Educational Services
Console I/O
The variable [Link] enables you to write to
standard output.
[Link] is an object of type PrintStream.
The variable [Link] enables you to read from
standard input.
[Link] is an object of type InputStream.
The variable [Link] enables you to write to
standard error.
[Link] is an object of type PrintStream.
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 3 of 17
Sun Educational Services
Writing to Standard Output
The println methods print the argument and a
newline character (\n).
The print methods print the argument without a
newline character.
The print and println methods are overloaded for
most primitive types (boolean, char, int, long,
float, and double) and for char[], Object, and
String.
The print(Object) and println(Object) methods
call the toString method on the argument.
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 4 of 17
Sun Educational Services
Reading From Standard Input
1
2
3
4
5
6
7
8
9
10
11
12
13
import [Link].*;
public class KeyboardInput {
public static void main (String args[]) {
String s;
// Create a buffered reader to read
// each line from the keyboard.
InputStreamReader ir
= new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(ir);
[Link]("Unix: Type ctrl-d to exit." +
"\nWindows: Type ctrl-z to exit");
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 5 of 17
Sun Educational Services
Reading From Standard Input
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
try {
// Read each input line and echo it to the screen.
s = [Link]();
while ( s != null ) {
[Link]("Read: " + s);
s = [Link]();
}
// Close the buffered reader.
[Link]();
} catch (IOException e) { // Catch any IO exceptions.
[Link]();
}
}
}
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 6 of 17
Sun Educational Services
Simple Formatted Output
You can use the formatting functionality as follows:
[Link](name count\n);
String s = [Link](%s %5d%n, user, total);
Common formatting codes are listed in this table.
Code
%s
Description
Formats the argument as a string, usually by calling the
toString method on the object.
%d %o %x Formats an integer, as a decimal, octal, or hexadecimal value.
%f %g
Formats a floating point number. The %g code uses scientific
notation.
%n
Inserts a newline character to the string or stream.
%%
Inserts the % character to the string or stream.
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 7 of 17
Sun Educational Services
Simple Formatted Input
The Scanner class provides a formatted input function.
A Scanner class can be used with console input
streams as well as file or network streams.
You can read console input as follows:
1
2
3
4
5
6
7
8
9
10
11
12
import [Link].*;
import [Link];
public class ScanTest {
public static void main(String [] args) {
Scanner s = new Scanner([Link]);
String param = [Link]();
[Link]("the param 1" + param);
int value = [Link]();
[Link]("second param" + value);
[Link]();
}
}
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 8 of 17
Sun Educational Services
Files and File I/O
The [Link] package enables you to do the following:
Create File objects
Manipulate File objects
Read and write to file streams
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 9 of 17
Sun Educational Services
Creating a New File Object
The File class provides several utilities:
File myFile;
myFile = new File("[Link]");
myFile = new File("MyDocs", "[Link]");
Directories are treated like files in the Java programming
language. You can create a File object that represents a
directory and then use it to identify other files, for example:
File myDir = new File("MyDocs");
myFile = new File(myDir, "[Link]");
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 10 of 17
Sun Educational Services
The File Tests and Utilities
File information:
String getName()
String getPath()
String getAbsolutePath()
String getParent()
long lastModified()
long length()
File modification:
boolean renameTo(File newName)
boolean delete()
Directory utilities:
boolean mkdir()
String[] list()
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 11 of 17
Sun Educational Services
The File Tests and Utilities
File tests:
boolean
boolean
boolean
boolean
boolean
boolean
boolean
exists()
canWrite()
canRead()
isFile()
isDirectory()
isAbsolute();
is Hidden();
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 12 of 17
Sun Educational Services
File Stream I/O
For file input:
Use the FileReader class to read characters.
Use the BufferedReader class to use the readLine
method.
For file output:
Use the FileWriter class to write characters.
Use the PrintWriter class to use the print and
println methods.
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 13 of 17
Sun Educational Services
File Input Example
A file input example is:
1
2
3
4
5
6
7
8
9
10
11
12
13
import [Link].*;
public class ReadFile {
public static void main (String[] args) {
// Create file
File file = new File(args[0]);
try {
// Create a buffered reader
// to read each line from a file.
BufferedReader in
= new BufferedReader(new FileReader(file));
String s;
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 14 of 17
Sun Educational Services
Printing a File
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Read each line from the file and echo it to the screen.
s = [Link]();
while ( s != null ) {
[Link]("Read: " + s);
s = [Link]();
}
// Close the buffered reader
[Link]();
} catch (FileNotFoundException e1) {
// If this file does not exist
[Link]("File not found: " + file);
} catch (IOException e2) {
// Catch any other IO exceptions.
[Link]();
}
}
}
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 15 of 17
Sun Educational Services
File Output Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import [Link].*;
public class WriteFile {
public static void main (String[] args) {
// Create file
File file = new File(args[0]);
try {
// Create a buffered reader to read each line from standard in.
InputStreamReader isr
= new InputStreamReader([Link]);
BufferedReader in
= new BufferedReader(isr);
// Create a print writer on this file.
PrintWriter out
= new PrintWriter(new FileWriter(file));
String s;
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 16 of 17
Sun Educational Services
File Output Example
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[Link]("Enter file text. ");
[Link]("[Type ctrl-d to stop.]");
// Read each input line and echo it to the screen.
while ((s = [Link]()) != null) {
[Link](s);
}
// Close the buffered reader and the file print writer.
[Link]();
[Link]();
} catch (IOException e) {
// Catch any IO exceptions.
[Link]();
}
}
}
Java Programming Language
Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2
Module 11, slide 17 of 17