Java Tutorial/Development/System Class — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:30, 31 мая 2010

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.



public static void arraycopy (Object source, int sourcePos, Object destination, int destPos, int length)



1
2
3
4


Calculate process elapsed time

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");
  }
}





Exiting a Java program

public class Main {
  public static void main(String[] args) {
    System.exit(0);
  }
}





Get Name of User Currently Logged In to the Computer

public class Main {
  public static void main(String[] args) {
    String currentUser = System.getProperty("user.name");
    System.out.println(currentUser);
  }
}





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.



public class MainClass {
  public static void main(String[] args) {
    String message = "qwer";
    System.out.print(message);
  }
}





println adds a line terminator at the end of the argument

public class MainClass{
  public static void main(String[] args){
     System.out.println(12);
     System.out.println("g");
     
  }
}



12
g


PrintStream has many print method overloads that accept different types

You can pass any primitive type to the print method:



public class MainClass {
  public static void main(String[] args) {
    System.out.print(12);
    System.out.print("g");
  }
}



12g


public static long nanoTime(): provide more precision, i.e. in nanoseconds

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");
  }
}



1095


Redirect the output of System.out to a file.

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");
  }
}





Returns all system properties

  1. The return value is a java.util.Properties object.
  2. The Properties class is a subclass of java.util.Hashtable



public class MainClass {
  public static void main(String[] args) {
    java.util.Properties properties = System.getProperties();
    properties.list(System.out);
  }
}



-- 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

public static long currentTimeMillis()





Terminates the running program and the current JVM

public static void exit (int status)





Terminate virtual machine using System class

public class Main {
  public static void main(String[] args) {
    System.exit(0);
  }
}





The err field represents a PrintStream object

Its purpose is to display error messages that should get immediate attention of the user.



public class MainClass {
  public static void main(String[] args) {
    System.err.println("You have a runtime error.");
  }
}



You have a runtime error.


The "in" field represents the standard input stream

You can use it to accept user keyboard input.



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) {
    }
  }
}