Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   4. Miscellaneous Java features  >   4.5. The class System

4.5. The class System
Last updated: 2 January 2013.

This tutorial will show you several features provided by the class System.

The class System cannot be instantiated. Instead, it offers a set of static fields and methods. In the next sections, you will see how to redirect the output and error streams, how to use the system properties and how to use the method currentTimeMillis. Also, the methods arraycopy and exit will be discussed.


4.5.1. How to redirect the output and error streams

You have probably already seen calls like this:

System.out.println("Printing to the standard output stream");
System.err.println
("Printing to the standard error stream");

The first statement prints the string argument to the standard output stream whereas the second statement prints to the standard error stream. In most systems, the standard output and error streams are by default displayed together in a terminal (Linux and Mac), command prompt (Windows) or in the output area of an IDE like Eclipse. You can redirect either streams to other output destinations by calling the methods setOut(PrintStream out) and setErr(PrintStream err). For example, the following statements redirect the standard output stream to the file out.txt and the standard error stream to the file err.txt:

System.setOut(new PrintStream("out.txt"));
System.setErr
(new PrintStream("err.txt"));

On the one hand, if you call the method printStackTrace provided by the class Exception (typically in a catch block), the stack trace will be printed to the file err.txt. On the other hand, calls to System.out.println will be redirected to the file out.txt.


4.5.2. How to use the system properties

The system properties are a set of system-dependent pieces of information like the operating system name, the version of the installed JRE, the current working directory, the file separator character and the line separator string. You can read the system properties by calling one of the methods getProperty or getProperties provided by the class System. You can also change the system properties by calling one of the methods setProperties or setProperty. However, developers generally never have to change the system properties.

To read a given system property, use the method getProperty(String key). The argument key is the name of the system property. You will find the complete list of system property names in the API documentation of the method getProperties. Some of the most commonly used system properties are:As an example, the following sample shows how to create in the current working directory a new directory named java in a system-independent manner:


String fileSeparator = System.getProperty("file.separator");
String currentDirectory = System.getProperty
("user.dir");
new File(currentDirectory + fileSeparator + "java").mkdir();


4.5.3. The method currentTimeMillis

The method currentTimeMillis returns the number of milliseconds since the epoch (midnight, January 1, 1970). Usually, the method currentTimeMillis is used to measure the execution time of a portion of code. Here is an example:

long start = System.currentTimeMillis();

// Execute code
// ...
// ...

long end = System.currentTimeMillis();

long executionTimeInMilliseconds = end - start;

// ...
// ...


4.5.4. The methods arraycopy and exit

If one day you have to copy a portion of an array into another array, instead of hand coding everything, remember that the class System provides what you need: the method arraycopy(Object src, int srcPos, Object dest, int destPos, int length) which is very easy to use.

The method exit terminates the JVM. Developers should be very careful when using exit. In particular, the method exit should never be called if you are writing code that is meant to be bundled in a larger application (for example, in the form of a JAR file). Instead, you should throw exceptions when necessary and let the larger application handle termination.


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   4. Miscellaneous Java features  >   4.5. The class System
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   4. Miscellaneous Java features  >   4.6. Properties files


Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7