World class Hard Drive Recovery and renowned raid recovery services

WestNIC provides reliable web hosting services

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.

Subscribe to this FAQ: RSS news feed

FAQ search

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.

Premium Content: Follow this link for subscription information More details available to subscribers:
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. Create a BufferedReader from a FileReader and use the readLine() method to acquire the data.

Premium Content: Follow this link for subscription information More details available to subscribers:
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.

Premium Content: Follow this link for subscription information More details available to subscribers:
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.

Premium Content: Follow this link for subscription information More details available to subscribers:
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.

Premium Content: Follow this link for subscription information More details available to subscribers:
How do I limit the scope of a file chooser?

Q: Is it possible to convert a string to an abstract 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 JDK 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.

Help request

Use the form below to submit a help request or general enquiry about the Code Style Web site. Read our guidelines on asking the right questions first.

Information: Your email address will not be mis-used. If you include your address you may be sent a personal reply, you will not be added to any mailing list unless you request it. Read the site privacy statement for details.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log