Java Reference
In-Depth Information
1 // Double space files specified on command line.
2
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.PrintWriter;
6 import java.io.IOException;
7 import java.util.Scanner;
8
9 public class DoubleSpace
10 {
11 public static void main( String [ ] args )
12 {
13 for( String fileName : args )
14 doubleSpace( fileName );
15 }
16
17 public static void doubleSpace( String fileName )
18 {
19 PrintWriter fileOut = null;
20 Scanner fileIn = null;
21
22 try
23 {
24 fileIn = new Scanner( new FileReader( fileName ) );
25 fileOut = new PrintWriter( new FileWriter( fileName + ".ds" ) );
26
27 while( fileIn.hasNextLine( ) )
28 {
29 String oneLine = fileIn.nextLine( );
30 fileOut.println( oneLine + "\n" );
31 }
32 }
33 catch( IOException e )
34 { e.printStackTrace( ); }
35 finally
36 {
37 if( fileOut != null )
38 fileOut.close( );
39 if( fileIn != null )
40 fileIn.close( );
41 }
42 }
43 }
figure 2.20
A program to double-space files
Search WWH ::




Custom Search