Java Reference
In-Depth Information
15
echoFixed(text, System.out);
16
}
17
}
18
19
public static void echoFixed(String text,
20
PrintStream output) {
21
Scanner data = new Scanner(text);
22
if (data.hasNext()) {
23
output.print(data.next());
24
while (data.hasNext()) {
25
output.print(" " + data.next());
26
}
27
}
28
output.println();
29
}
30 }
Consider the following input file:
four score and
seven years ago our
fathers brought forth on this continent
a new nation, conceived in liberty
and dedicated to the proposition that
all men are created equal
Using this input, the program produces the following output file, called words2.txt :
four score and
seven years ago our
fathers brought forth on this continent
a new nation, conceived in liberty
and dedicated to the proposition that
all men are created equal
The output also appears in the console window.
Guaranteeing That Files Can Be Read
The programs we have studied so far assume that the user will provide a legal file
name. But what if the user accidentally types in the name of a file that doesn't exist
or that can't be read for some reason? In this section we explore how to guarantee
that a file can be read.
Let's explore how you might handle the task of prompting the user for a file name
in the console window. If the user does not input a legal file name, you can keep
 
Search WWH ::




Custom Search