Java Tutorial/Development/System Class
Содержание
- 1 Array copy method
- 2 Calculate process elapsed time
- 3 Exiting a Java program
- 4 Get Name of User Currently Logged In to the Computer
- 5 java.lang.System
- 6 println adds a line terminator at the end of the argument
- 7 PrintStream has many print method overloads that accept different types
- 8 public static long nanoTime(): provide more precision, i.e. in nanoseconds
- 9 Redirect the output of System.out to a file.
- 10 Returns all system properties
- 11 Returns the computer time in milliseconds
- 12 Terminates the running program and the current JVM
- 13 Terminate virtual machine using System class
- 14 The err field represents a PrintStream object
- 15 The "in" field represents the standard input stream
Array copy method
Copy the content of an array (source) to another array (destination), beginning at the specified position, to the specified position of the destination array.
<source lang="java">
public static void arraycopy (Object source, int sourcePos, Object destination, int destPos, int length)</source>
1 2 3 4
Calculate process elapsed time
<source lang="java">
public class Main {
public static void main(String[] args) { long start = System.nanoTime(); System.out.println("Start: " + start); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { System.out.println("asdf"); } } long end = System.nanoTime(); System.out.println("End : " + end); long elapsedTime = end - start; System.out.println(elapsedTime + " nano seconds"); }
}</source>
Exiting a Java program
<source lang="java">
public class Main {
public static void main(String[] args) { System.exit(0); }
}</source>
Get Name of User Currently Logged In to the Computer
<source lang="java">
public class Main {
public static void main(String[] args) { String currentUser = System.getProperty("user.name"); System.out.println(currentUser); }
}</source>
java.lang.System
The System class is a final class that exposes useful static fields. The static methods can help you with common tasks.
The out field represents the standard output stream which by default is the same console.
<source lang="java">
public class MainClass {
public static void main(String[] args) { String message = "qwer"; System.out.print(message); }
}</source>
println adds a line terminator at the end of the argument
<source lang="java">
public class MainClass{
public static void main(String[] args){ System.out.println(12); System.out.println("g"); }
}</source>
12 g
PrintStream has many print method overloads that accept different types
You can pass any primitive type to the print method:
<source lang="java">
public class MainClass {
public static void main(String[] args) { System.out.print(12); System.out.print("g"); }
}</source>
12g
public static long nanoTime(): provide more precision, i.e. in nanoseconds
<source lang="java">
public class MainClass {
public static void main(String[] args) { long start = System.nanoTime(); // block of code to time long end = System.nanoTime(); System.out.println("It took " + (end - start) + " nanoseconds"); }
}</source>
1095
Redirect the output of System.out to a file.
<source lang="java">
import java.io.File; import java.io.IOException; import java.io.PrintStream; public class MainClass {
public static void main(String[] args) { File file = new File("C:\\temp\\debug.txt"); try { PrintStream ps = new PrintStream(file); System.setOut(ps); } catch (IOException e) { } System.out.println("To File"); }
}</source>
Returns all system properties
- The return value is a java.util.Properties object.
- The Properties class is a subclass of java.util.Hashtable
<source lang="java">
public class MainClass {
public static void main(String[] args) { java.util.Properties properties = System.getProperties(); properties.list(System.out); }
}</source>
-- listing properties -- java.runtime.name=Java(TM) 2 Runtime Environment, Stand... sun.boot.library.path=C:\Java_Dev\sdk\jdk\jre\bin java.vm.version=1.5.0-b64 ...
Returns the computer time in milliseconds
<source lang="java">
public static long currentTimeMillis()</source>
Terminates the running program and the current JVM
<source lang="java">
public static void exit (int status)</source>
Terminate virtual machine using System class
<source lang="java">
public class Main {
public static void main(String[] args) { System.exit(0); }
}</source>
The err field represents a PrintStream object
Its purpose is to display error messages that should get immediate attention of the user.
<source lang="java">
public class MainClass {
public static void main(String[] args) { System.err.println("You have a runtime error."); }
}</source>
You have a runtime error.
The "in" field represents the standard input stream
You can use it to accept user keyboard input.
<source lang="java">
import java.io.IOException; public class MainClass {
public static void main(String[] args) { try { char c = (char) System.in.read(); while (c != "\r") { System.out.println(c); c = (char) System.in.read(); } } catch (IOException e) { } }
}</source>