CSS font stacks & developer FAQs with Web standards

WestNIC provides reliable web hosting services

Fastwebhost offers cheap web hosting & reseller hosting services

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.

What if you were One Percent Better? Java ebooks for iPad, Kindle, Nook and Sony Reader

Read and write files

Q: How can I divert console output to a file?

A: The java.lang.System class holds a static reference to the console output stream in its System.out field, which is an instance of java.io.PrintStream. The System.setOut(PrintStream) method allows you to assign a different output to the reference. In this case you can construct a PrintStream from a FileOutputStream, as below.

… full answer hidden

Premium members click below for full answer
How can I divert console output to a file?

Q: How can I generate an array from a list?

A: The answer to your question would depend on the format of the list, which is perhaps stored in a file? If there is a consistent character pattern that separates the list items, you might use a StringTokenizer to capture them. The example below creates a BufferedReader from a FileReader and uses the readLine() method to acquire the data.

… full answer hidden

Premium members click below for full answer
How can I generate an array from a list?

File system navigation

Q: How can I get the full path of Explorer.exe?

A: Firstly, you cannot generally obtain this reference from an applet loaded via the Internet because the Java sandbox will not permit this type of system access.

… full answer hidden

Premium members click below for full answer
How can I get the full path of Explorer.exe?

Q: How can I get the path to an installed program?

A: You can check for programs added to the host system's path environment variable through the java.lang.System class' getenv() method, or getenv(String), provided the application has the necessary security permission. The getenv() method returns a map of all environment variables in the current user session, getenv(String) returns the String value of the variable named by the argument, if it exists. You can extract all the paths in the path environment variable using a StringTokenizer using the system specific path separator as the delimiter, as below.

… full answer hidden

Premium members click below for full answer
How can I get the path to an installed program?

Q: How do I limit the scope of a file chooser?

A: The general mechanism for limiting file selection is to use a FileFilter. You could write a custom filter that only accepts files in a specified directory for instance. This would not stop users from browsing the file system, but would ensure that no other files could be selected.

… full answer hidden

Premium members click below for full answer
How do I limit the scope of a file chooser?

Q: Is it possible to convert a string to an abstract file path?

A: The java.io.File class has various methods for creating abstract file paths, which can then be created on the file system or used to test whether the actual file paths exist. The simple constructor File(String) takes an argument that is a complete abstract path name. In the File(File, String) constructor, the File argument is a parent directory reference and the String is a path name under that directory. See the Java Development Kit Application Programming Interface (API) documentation for further details about listing, creating, testing, comparing, deleting and setting the properties of files.

Input and output streams

Q: Why are streams used in Java?

A: In Java streams are used to process input and output data as a sequence of bytes, which does not assume a specific character content or encoding. Byte content can be read from network sources, files and other sources, and bytes copied between multiple inputs and outputs.

Streams types can be sub-classed to add filtering, to mark a point in the stream and re-read those bytes, or to skip a number of bytes. Streams also enable serlializable objects to stored and re-constructed using ObjectInputStream and ObjectOutputStream types.

Q: What does "broken pipe" mean?

A: A pipe is an input/output link between two programs, commonly where you use the output from one program as the input for another program. A broken pipe means that the linkage between the output and input is interrupted, the reasons vary. For example, the feeder program may throw an error and terminate unexpectedly, intermediate source or output files may not be created successfully, or key resources become unavailable during the process. You may also get problems with the amount of swap file storage or overall memory usage approaches its limits.

Q: Is it best to customise state with Serializable or Externalizable?

A: When a class is the marked as Serializable it signifies that it is suitable for object serialization using the built-in ObjectOutputStream writeObject(Object) method without modification. It is the programmer's responsibility to ensure this is the case and that it can be restored by the inverse readObject(Object) method.

Default object serialization methods have a great advantage because they take care of the whole process without special programming. The state of superclass fields are automatically discovered and incorporated in serial data streams using reflection, which can affect performance but is normally a great convenience.

If a Serializable class requires special measures to properly save and restore its internal state, it should define special read/write methods, as below.

… full answer hidden

Premium members click below for full answer
Is it best to customise state with Serializable or Externalizable?

Home · Web fonts · Font stacks · FAQs · Java · CSS · Javascript · HTML · Site manager