Examples from Java I/O

The complete set of examples is also available for anonymous ftp from ftp://metalab.unc.edu/pub/languages/java/javafaq/

Please feel free to use any fragment of this code you need in your own work. As far as I am concerned, it's in the public domain. No permission is necessary or required. Credit is always appreciated if you use a large chunk or base a significant product on one of my examples, but that's not required either.

The examples that are likely to be reused are placed in the com.macfaq package. Examples that merely demonstrate a point or run as a stand alone program or applet are generally placed in the default package. This means you will need to pay attention to classpath issues when compiling files. Simply typing

% javac *.java
will generally fail to compile those files that depend on classes in the com.macfaq package, generally because the source code file in the local directory takes precedence over the previously compiled file found in the class path. Typing

% javac -d . *.java
will generally be more successful. Depending on how you organize your source code files, in some cases you may need to move source code files from one directory to another where the compiler can't find them to avoid compiler error messages that claim something like:

error: File .\JavaFilter.java does not contain type JavaFilter as expected. Please adjust the class path so that the file does not appear in the unnamed package
This error message is misleading. What it really means is that the JavaFilter class is in fact not in the unnamed package. When the compiler is compiling a different file that uses the short, non-package qualified name JavaFilter instead of the full, package-qualified name com.macfaq.swing.filechooser.JavaFilter, the compiler first looks for a class in the unnamed package named JavaFilter. If it finds a file named JavaFilter.java in the current directory, it will try to compile that, which will fail because JavaFilter.java contains com.macfaq.swing.filechooser.JavaFilter and not JavaFilter. The solution is much simpler than the explanation: first compile JavaFilter.java and place the resulting JavaFilter.class in the right place in your class path; move JavaFilter.java out o the current directory; and then compile the file that depends on JavaFilter.java.

To run a program in a non-default package from the command line, you'll need to use the full, package qualified name. For example, it's not enought to simply type

% java StreamCopier
You have to type

% java com.macfaq.io.StreamCopier

A JAR archive (javaio.jar) is available that includes compiled versions of all classes in the com.macfaq packages. In Java 1.1 you can add this archive to your classpath to have access to all the com.macfaq classes. For example,

% setenv CLASSPATH $CLASSPATH:/path/to/directory/containing/javaio.jar

In JDK 1.2, you can simply copy the javaio.jar file to your jre/lib/ext directory.

Note that if you try to download this JAR archive to your computer using a web browser, it may give you various errrors about security violations or corrupt JAR formats. This is inaccurate. Your browser is incorrectly acting as if you want to load a JAR archive for an applet rather than simply download a file. You may be able to avoid the problem by right-clicking on the link and selecting "Save this link as..." or the equivalent rather than simply clicking on the link in your browser. If that fails, you can try downloading it from ftp://metalab.unc.edu/pub/languages/java/javafaq/ using an FTP program instead of a Web browser.

[ Java I/O Home Page ]

[ Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1999, 2001 Elliotte Rusty Harold
Last Modified April 18, 2001