FileDemo.java

Download FileDemo.java

 1: // FileDemo.java - Shows how to use java.io classes to read and
 2: // write files.
 3: //
 4: // Written 2009 by Wayne Pollock, Tampa Florida
 5: //
 6: 
 7: import java.io.*;
 8: import javax.swing.JOptionPane;
 9: 
10: public class FileDemo
11: {
12:    public static void main ( String [] args )
13:    {
14:       JOptionPane.showMessageDialog( null, "The system default encoding is " +
15:          getSystemDefaultEncoding() );
16:       writeFile( "lowercase-UTF8.txt" );
17:       readFile( "UTF-8-demo.txt" );
18:    }
19: 
20:    /**
21:     * Windows uses the phrase "code page" for encoding, and
22:     * uses a Micorsoft naming scheme.  For example "Cp1252" is
23:     * a code for "Windows Western Europe / Latin-1".
24:     *
25:     * A table of encodings may be found at:
26:     *    http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
27:     *
28:     * @return The value of the system property "file.encoding"
29:     */
30: 
31:    public static String getSystemDefaultEncoding ()
32:    {
33:       String encoding = System.getProperty( "file.encoding" );
34:       return encoding;
35:    }
36: 
37:    /** Writes all the lower case letters of the Unicode alphabet to a file.
38:     * The "UTF-8" encoding used has the property of representing ASCII text
39:     * as itself (one byte), and other characters as two or three bytes.
40:     *
41:     * @param fileName The name of the file to create.
42:     */
43: 
44:    public static void writeFile ( String fileName )
45:    {
46:       try
47:       {
48:          FileOutputStream fos = new FileOutputStream( fileName );
49:          OutputStreamWriter osw = new OutputStreamWriter( fos, "UTF8" );
50:          for ( int c = '\u0000'; c <= '\uffff'; ++c )
51:          {
52:             if ( ! Character.isLowerCase( (char)c ) )
53:                continue;
54:             osw.write( c );
55:          }
56:          osw.close();
57:       }
58:       catch ( IOException e )
59:       {
60:          System.err.println( e );
61:       }
62:    }
63: 
64:    /** Reads all the lower case letters from a file.
65:     * The "UTF-8" encoding used has the property of representing ASCII text
66:     * as itself (one byte), and other characters as two or three bytes.
67:     *
68:     * @param fileName The name of the file to read.
69:     */
70: 
71:    public static void readFile ( String fileName )
72:    {
73:       final int MAX = 100, EOF = -1;
74:       int ch;
75:       StringBuilder sb = new StringBuilder( MAX );
76:       try
77:       {
78:          FileInputStream fis = new FileInputStream( fileName );
79:          InputStreamReader isr = new InputStreamReader( fis, "UTF8" );
80:          while ( (ch = isr.read() ) != EOF )
81:          {
82:             if ( Character.isLowerCase( (char)ch ) )
83:                sb.append( (char) ch );
84:             if ( sb.length() >= MAX )
85:                break;
86:          }
87:          isr.close();
88:       }
89:       catch ( IOException e )
90:       {
91:          System.err.println( e );
92:       }
93: 
94:       JOptionPane.showMessageDialog( null, "First " + MAX +
95:          " lowercase letters found in file \"" + fileName + "\":\n" +
96:          sb.toString() );
97:    }
98: }