Java Reference
In-Depth Information
8 public static void main(String[] args)
9 throws FileNotFoundException
10 {
11 Scanner console = new Scanner(System.in);
12 System.out.print( ÐInput file: Ñ );
13 String inputFileName = console.next();
14 System.out.print( ÐOutput file:Ñ );
15 String outputFileName = console.next();
16
17 FileReader reader = new
FileReader(inputFileName);
18 Scanner in = new Scanner(reader);
19 PrintWriter out = new
PrintWriter(outputFileName);
20 int lineNumber = 1 ;
21
22 while (in.hasNextLine())
23 {
24 String line = in.nextLine();
25 out.println( Ð/* Ñ + lineNumber + Ð */
Ñ + line);
26 lineNumber++;
27 }
28
29 out.close();
30 }
31 }
499
500
S ELF C HECK
1.
What happens when you supply the same name for the input and output
files to the LineNumberer program?
2.
What happens when you supply the name of a nonexistent input file to
the LineNumberer program?
C OMMON E RROR 11.1: Backslashes in File Names
When you specify a file name as a constant string, and the name contains
backslash characters (as in a Windows file name), you must supply each backslash
twice:
in = new FileReader("c:\\homework\\input.dat");
Search WWH ::




Custom Search