IO Section Index | Page 2
Why did the toURL() method of File get deprecated in Java 6? Should I stop using it now.
The toURL() method of File does not properly escape characters that aren't valid in a URL. You can write better code today without waiting for Java 6 by calling the toURL() method after calling to...more
How do I set the execute attribute of a file?
Assuming you are on a platform that supports the attribute, you can call the setExecutable() method of File. Of course, you'll need to wait for Java 6 to do this. Prior versions of standard Java d...more
How do I print high-order characters to the console? Whenever I use System.out.println(), it chops off anything above seven bits.
System.out is an OutputStream. You need to work with a Writer object. With Java 6, you would use the printf() method of the new Console class, which is returned by the console() method of System.more
How can I accept a password from the console without an echo? - Java 6
How can I accept a password from the console without an echo?
How do I find out the default character set for the JVM?
Added to JDK 5.0, you can call the defaultCharset() method of the CharSet class, found in the java.nio.charset package. This is apt to depend upon the locale and character set of the underlying sy...more
How do I print to a file?
Typically, you would create a FileOutputStream and pass it to the PrintStream constructor. Starting with 5.0, you can pass the File or String name for file directory to the PrintStream constructor.more
Why would I want to use the Appendable interface?
The Appendable interface and its append() methods are implemented in classes that support adding characters to the end of an object, typically a stream. Indirectly used through the Formatter class...more
What is the deal with the Closeable and Flushable interfaces?
The ability to close() or flush() an object (typeically a stream) has been factored out into single method interfaces, Closeable and Flushable, respectively. Given that you sometimes need to opera...more
Why can't I use Unicode supplementary characters in JDK 1.4?
JDK 5.0 added support for code points above U+FFFF. Additional information is available from Supplementary Characters in the Java Platform.more
How do I read formatted input from the command line?
The java.util.Scanner constructor takes a file, channel, input stream, or reader as an argument and lets you read an int (with nextInt()), read a line (with nextLine()), or any other data type wit...more
How do I print a JTable?
JDK 5.0 provides built in support for printing tables. Just call the print() method of JTable. Here's an example of such.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
impor...more
How do I use the java.util.Formatter class to format output?
There are at least three ways to generate formatted output:
Use the format() method of an instance of Formatter.
Use the format()/printf method of an OutputStream, as in System.out.format("The t...more
How do I do C-style formatted printing, like with printf?
The java.util.Formatter class was introduced in JDK 5.0 and offers locale-sensitive formatting of numbers and strings.
What's the difference between a StringBuffer and StringBuilder?
The StringBuilder class was introduced with JDK 5.0. Essentially, a StringBuffer is a thread-safe version of StringBuilder. If you are only adding/removing characters from a single-thread, the Str...more
How do I print a PDF file using the new JDK 1.4 printing API?
Until someone provides a PDF print service, you can't. The standard runtime does not provide one. Just because you can specify a DocFlavor of PDF (DocFlavor.STRING.INPUT_STREAM.PDF) doesn't mean y...more