Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Java Tip 64: Get rid of those empty black squares in your text editor

Do you go nuts when your editor shows you unreadable text? The application in this article will help you keep your cool!

Many programmers spend hours analyzing the code written by excellent programmers to improve their own style and learn new programming tricks. With the upcoming release of JDK 1.2, many who look over the supplied source code will encounter a problem on Windows 95/Windows NT 4.0 systems. After unpacking the sources of the Java API, the standard Windows editor shows unreadable text. If you edit the file Applet.java, for example, you'll see a frame that looks like the following snapshot:

Here, the text lines are just lined up using a black square as line separator symbol. When the chain of lines is getting too long, the output is continued in a new line of the frame.

This Java Tip will help you understand and overcome problems with the representation of the line separator on different systems. It introduces a conversion program called EOLConverter (EOL is an abbreviation for "end of line,") which enables you to get a frame that looks like this:

Now the black squares have vanished into thin air and each line of text is printed on its own line. To see this, just compare the colored regions in this frame to the corresponding regions in the last frame.

Converting the line separator

The conversion of the line separator chosen on the system of the text's author to the line separator used on your system is achieved by the application EOLConverter.java.

Copy this file to the directory housing files you want to convert. Compile the source code by executing the command

javac EOLConverter.java


and start the application by invoking the Java interpreter through the command

java EOLConverter


Please note that the application only converts files with the extensions .htm, .html, .java, and .txt, and that the files in the subdirectories of all levels in your current directory are converted, too.

In the section Analyzing classes, you'll learn how the classes BufferedWriter and BufferedReader in the paackage java.io.* manage the line separator.

In the section Understanding EOLConverter, you'll find a detailed discussion of the statements and the execution flow performed in this application.

Analyzing classes

Now we proceed with the discussion of two essential classes we'll use in our application. The source code for these classes is shipped with the JDK 1.2, and is installed automatically if you don't deactivate the corresponding checkbox during the installation process.

Move to the directory jdk1.2 and unjar the file src.jar by executing the command:

jar -xvf src.jar


You should now find the source files of the API 1.2 in the directory jdk1.2/src.

Edit the file BufferedWriter.java, which is stored in the directory jdk1.2/src/java/io.

  1. At the beginning of the class body, the following string object is defined:

    private String lineSeparator;
    


  2. During the execution of the constructor the object lineSeparator is initialized by

    lineSeparator = (String) java.security.AccessController.doPrivileged(
    
    new sun.security.action.GetPropertyAction("line.separator"));


    Hence the line separator string is defined by the system property line.separator which depends on your system. For example, the value of the line separator is \n on Unix systems and \r\n on Windows systems.

    1 | 2 | 3 |  Next >
    Resources
    • For more information about the package java.io.*, see the following section of the Java Tutorial http://java.sun.com/docs/books/tutorial/essential/io/index.html
    • Browse JDK 1.2 documentation for further functionality of the BufferedReader class at http://java.sun.com/products/jdk/1.2/docs/api/java/io/BufferedReader.html
    • and the BufferedWriter class at http://java.sun.com/products/jdk/1.2/docs/api/java/io/BufferedWriter.html
    • German readers of JavaWorld are invited to consult the chapter "Eingabe und Ausgabe" of my online course about Java http://www.math.uni-siegen.de/willms/java/index.html