Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
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!

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 3

  • Line 03: The interfacee FilenameFilter is implemented in the class EOLConverter so that only files with the extensions .htm, .html, .java, and .txt are converted. The method accept of this interface is overwritten from lines 09 to 17.

  • Lines 05 to 07: During the execution of the method main, the method convertDirectory is invoked. The string object (".") represents the current directory (in which you have started the EOLConverter program).

  • Lines 09 to 17: The method accept is the only method that needs to be overwritten if you implement a FilenameFilter. In line 10, the complete path of a directory or file is determined. If the path represents a directory, the value of the boolean variable in line 11 is true and the method return in line 16 returns true. During the execution of the statements from lines 12 to 15, we check to see whether the file has one of the above extensions. At this point, you are invited to also accept files with other extensions.

  • Lines 19 to 31: In line 21, the program returns a list of all directories and all files with the above extensions. The method list gets the implemented FilenameFilter via the construction of an object of the current class as its argument. The previously discussed method accept is called automatically for you at this point.

    After that, we examine all list entries to determine whether each is a directory or a file. If the list entry is a directory, we execute the current block again by recalling the method in line 19, but now with our current entry as the new argument. This is the most important statement of the program EOLConverter, because now all the files in the child directories are converted. However, this is also a dangerous statement; you may run into memory problems if you have too many child directories to convert. If the list entry isn't a directory, the method convertFile is invoked to convert the line separator.

    The statement in line 23 is set apart with comment tags because we call a method of the class PrintStream on the object out of the class System. To execute this statement, your operating system must have a standard output device. If this is the case, feel free to uncomment the statement and observe the application doing its job.

  • Lines 33 to 52: The conversion of the line separator is automatically performed when we read a line from the file and print it to a temporary file. This effect was discussed in the section Analyzing classes above. After the closure of the streams, we delete the file with the wrong line separators and rename the temporary file to the previous file.


The execution flow of the application can be summarized as follows: First, create a list of all directories and all files with the specified extensions. Then, determine for each list entry whether it is a directory or a file. If a list entry is a file, the line separator is converted by shipping its text lines into a temporary file, which is renamed after the conversion process has finished. If a list entry is a directory, you must create a new list for this directory and repeat the process.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
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