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