Chapter 19
Input and Output
19.1 Dialog Boxes
- The ‘javax.swing.JOptionPane’ class offers dialog box methods.
- Output messages
JOptionPane.showMessageDialog (null, <message> )
import javax.swing.JOptionPane;
class prg{
public static void main(String[] args){
JOptionPane.showMessageDialog (null, "My message");
System.exit(0);
} }
- Input messages
<String variable> = JOptionPane.showInputDialog (<message> )
import javax.swing.JOptionPane;
class prg{
public static void main(String[] args){
String input;
input = JOptionPane.showInputDialog
("Please enter a value ");
System.exit(0);
} }
- The ‘System.exit(0);’ method is required for terminating the execution.
19.2 Standard Output
-
- System.out.println
- System.out.print
- The ‘System’ class introduces a class field named ‘out’, set to direct text from the program to an external
destination. The destination is standard for the computing environment in use. Typically, it is the screen of a
monitor.
- The class field ‘out’ references an object of class type ‘PrintStream’.
- The ‘PrintStream’ class introduces ‘print’ and ‘println’ instance methods
- The ‘print’ and ‘println’ methods provide to ‘System.out’ the text for the output.
System.out.println("The class of " + System.out +
" is " + (System.out).getClass().getName());
// The class of java.io.PrintStream@bf786fde is java.io.PrintStream
19.3 Standard Input
- The ‘System’ class introduces a class field named ‘in’. The field is set to direct text into the program from an
external source that is standard for the computing environment in use. Typically, this source is a keyboard.
- The ‘read’ method provides the next byte from the input, returned as a value of type ‘int’.
- The ‘read’ method throws a ‘java.io.IOException’ exception, when the method encounters communication
problems. The exception must be caught.
class prg {
public static void main(String args[])
throws java.io.IOException {
int c;
c = System.in.read();
System.out.print( c + " : " + (char)c );
c = System.in.read();
System.out.print( c + " : " + (char)c );
} }
19.4 Writing into Files
try{
FileWriter file = new FileWriter( "try.txt" );
PrintWriter myout = new PrintWriter( file );
myout.println("Hello, file!");
myout.close();
} catch(java.io.IOException e){
// do something
}
java.io: FileWriter, PrintWriter
19.5 Reading from Files
try{
int c;
FileReader myIn = new FileReader( "try.txt" );
while( (c=myIn.read()) != -1 ){
System.out.print( (char) c );
}
System.out.println();
} catch(java.io.IOException e){
// do something
}
try{
String s;
FileReader fr = new FileReader( "try.txt" );
BufferedReader myIn = new BufferedReader( fr );
while( (s=myIn.readLine()) != null ){
System.out.println( s );
}
} catch(java.io.IOException e){
// do something
}
java.io: FileReader, BufferedReader
19.6 Assignment #24: I/O
Tentative assignment; subject to change
Write a program that does the following.
- Prompts the user for an input file name through a dialog box.
- Prompts the user for an output file name through a dialog box.
- Copies the input file into the output file, subject to the removal of the space characters listed below from each
line.
- The leading space characters
- The trailing space characters
- The space characters that are preceded by space characters
Example. An input
This is a
fragment of
text.
should result in the following output.
This is a
fragment of
text.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A
Assume ‘lab24’ for the submit program.
19.7 Assignment #25: I/O + arrays
Tentative assignment; subject to change
Write a program that reads 50 integer values from a file and prints out the following information
- The list of input values
- The list of input values in reverse order
- The largest value in the input
- The smallest value in the input
- A message stating whether the list of input values is equal to the list of input values in reverse order.
Assume ‘lab25’ for the submit program.
Q&A