IO Section Index | Page 13
How do I create directories in the local file system?
The File class includes mkdir() and mkdirs() methods that should help you. mkdir() will only make the file as a directory, at the last level. What mkdirs() does is create all parent directories, t...more
How can I accept a password from the console without an echo?
You can't. Java provides no way to disable echo when reading from System.in.
How can I implement a FileFilter that supports wildcards in the file pattern?
You can find an implementation of FilenameFilter that supports this at http://www.jzoo.com/java/wildcardfilter/index.html. This should be easily modifiable to a FileFilter.more
How can I save/load application settings that I would normally use .ini files or the Windows Registry?
You can use Java property files for storing settings for an application. Use the java.util.Properties class, which includes a load and store method.
You can also use a PropertyResourceBundle, but ...more
How do I append to end of a file in Java?
You can use java.io.RandomAccessFile and something like the following:
try {
RandomAccessFile raf =
new RandomAccessFile("filename.txt", "rw");
raf.skipBytes( (int)ra...more
How can I set file permissions for files created from Java?
In pure Java code, you can only specify the read, write, or read & write file modes using the java.io.File class.
Unfortunately, for operating systems like Linux, that means that you don't ha...more
How do I get FilenameFilter to work on my FileDialog?
Basically, it doesn't work on Windows machines.
The JFileDialog provides a better alternative that supports
filters, if you can use the Swing component set.
See Bug 4031440 for more information...more
How do I change what standard output or standard error goes to so it can go somewhere other than the console?
The System class allows you to reassign where these streams go to with either the static setOut(PrintStream) method or the setErr(PrintStream) method.
What happened to printf or how do I format the numbers I need to print?
The Java libraries and println() method provide no direct equivalent to printf/sprintf. While Acme labs provides an alternative at http://www.acme.com/java/software/Acme.Fmt.html, the NumberFormat...more
How do I work with the StreamTokenizer to get number and word tokens from a file?
After opening up a FileReader, you get the next token and switch based upon which type of token you have:
FileReader reader = new FileReader(filename);
BufferedReader br = new BufferedReader(read...more
Can I use a BufferedOutputStream with an ObjectOutputStream to serialize an object?
Yes. In fact, it is recommended that
you buffer all your input and
output unless you have a good reason
not to.
Here's an example of how to do this:
...
String myobject = new String("my...more
How can I compress my data to save bandwidth when sending across a socket?
The GZIPInputStream and GZIPOutputStream classes found in the java.util.zip package compress data with the GZIP file format, as defined by RFC 1952 (ftp://ftp.uu.net/graphics/png/documents/zlib/zd...more