Java by API/java.lang/System
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 System: arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
- 2 System: clearProperty(String key)
- 3 System: console()
- 4 System: currentTimeMillis()
- 5 System.exit(int status)
- 6 System: getenv(String key)
- 7 System: getProperties()
- 8 System: getProperty(String key, String def)
- 9 System: getProperty(String name)
- 10 System.getProperty ("user.dir")
- 11 System.identityHashCode(Object x)
- 12 System.in
- 13 System.in.read()
- 14 System: nanoTime()
- 15 System.out
- 16 System.setErr(PrintStream err)
- 17 System: setIn(InputStream in)
- 18 System: setOut(PrintStream err)
- 19 System: setProperty(String key, String value)
System: arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
/*
* Output:
array2: 0 1 2 3 4 0 0 0 0 0
*/
public class MainClass {
public static void main(String args[]) {
int array1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int array2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
System.arraycopy(array1, 0, array2, 0, 5);
System.out.print("array2: ");
System.out.print(array2[0] + " ");
System.out.print(array2[1] + " ");
System.out.print(array2[2] + " ");
System.out.print(array2[3] + " ");
System.out.print(array2[4] + " ");
System.out.print(array2[5] + " ");
System.out.print(array2[6] + " ");
System.out.print(array2[7] + " ");
System.out.print(array2[8] + " ");
System.out.println(array2[9]);
}
}
System: clearProperty(String key)
public class Main {
public static void main(String[] a) {
String oldValue = System.clearProperty("java.class.path");
System.setProperty("user.dir", "C:/MyProg");
System.out.println(System.getProperty("user.dir"));
}
}
System: console()
import java.io.Console;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String args[]) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String login = c.readLine("Enter your login: ");
char[] oldPassword = c.readPassword("Enter your old password: ");
if (verify(login, oldPassword)) {
boolean noMatch;
do {
char[] newPassword1 = c.readPassword("Enter your new password: ");
char[] newPassword2 = c.readPassword("Enter new password again: ");
noMatch = !Arrays.equals(newPassword1, newPassword2);
if (noMatch) {
c.format("Passwords don"t match. Try again.%n");
} else {
change(login, newPassword1);
c.format("Password for %s changed.%n", login);
}
Arrays.fill(newPassword1, " ");
Arrays.fill(newPassword2, " ");
} while (noMatch);
}
Arrays.fill(oldPassword, " ");
}
// Dummy verify method.
static boolean verify(String login, char[] password) {
return true;
}
// Dummy change method.
static void change(String login, char[] password) {
}
}
System: currentTimeMillis()
/*
* Output:
1145995967880
*/
public class MainClass {
public static void main(String args[]) {
System.out.println(System.currentTimeMillis());
}
}
System.exit(int status)
public class MainClass {
public static void main(String[] args) throws Exception {
Object f = new Object() {
public void finalize() {
System.out.println("Running finalize()");
}
};
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
f = null;
System.gc();
System.out.println("Calling System.exit()");
System.exit(0);
}
}
System: getenv(String key)
/*
System.getenv("PATH") = C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\nls;....
* */
public class MainClass {
public static void main(String[] a) throws Exception {
System.out.println("System.getenv(\"PATH\") = " + System.getenv("PATH"));
}
}
System: getProperties()
/*
* Output:
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
java.vm.version=1.5.0-b64
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.ru/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.5.0-b64
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
...
...
*/
import java.util.Properties;
public class MainClass {
public static void main(String[] args) {
Properties properties = System.getProperties();
properties.list(System.out);
}
}
System: getProperty(String key, String def)
public class Main{
public static void main(String[] args){
System.out.println(System.getProperty("password","defaultPassword"));
}
}
System: getProperty(String name)
/*
* Output:
C:\Java_Dev\workspace\cal
*/
public class MainClass {
public static void main(String args[]) {
System.out.println(System.getProperty("user.dir"));
}
}
System.getProperty ("user.dir")
public class Main{
public static void main(String[] args){
System.out.println(System.getProperty ("user.dir"));
}
}
System.identityHashCode(Object x)
import java.io.File;
public class Main {
public static void main(String[] argv) throws Exception {
File file1 = new File("a");
File file2 = new File("a");
File file3 = new File("b");
int ihc1 = System.identityHashCode(file1);
System.out.println(ihc1);
int ihc2 = System.identityHashCode(file2);
System.out.println(ihc2);
int ihc3 = System.identityHashCode(file3);
System.out.println(ihc3);
}
}
System.in
/*
* Output:
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainClass {
public static void main(String args[]) {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
System.out.print("Radius? ");
String str = br.readLine();
double radius;
try {
radius = Double.valueOf(str).doubleValue();
} catch (NumberFormatException nfe) {
System.out.println("Incorrect format!");
continue;
}
if (radius <= 0) {
System.out.println("Radius must be positive!");
continue;
}
double area = Math.PI * radius * radius;
System.out.println("Area is " + area);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.in.read()
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
int inChar;
System.out.println("Enter a Character:");
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
}
System: nanoTime()
public class Main {
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");
}
}
System.out
/*
* Output:
true
A
500
40000
45.67
45.67
Hello
99
*/
import java.io.PrintWriter;
public class MainClass {
public static void main(String args[]) {
try {
PrintWriter pw = new PrintWriter(System.out);
// Experiment with some methods
pw.println(true);
pw.println("A");
pw.println(500);
pw.println(40000L);
pw.println(45.67f);
pw.println(45.67);
pw.println("Hello");
pw.println(new Integer("99"));
// Close print writer
pw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
System.setErr(PrintStream err)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;
public class MainClass {
public static void main(String[] args) {
try {
String FILE = "c:\\systemin.txt";
FileOutputStream outStr = new FileOutputStream(FILE, true);
PrintStream printStream = new PrintStream(outStr);
System.setErr(printStream);
Timestamp now = new Timestamp(System.currentTimeMillis());
System.out.println(now.toString() + ": This is text that should go to the file");
outStr.close();
printStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
System.exit(-1);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
}
System: setIn(InputStream in)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class Main {
public static void main(String[] args) throws IOException {
PrintStream console = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
System.setIn(in);
System.setOut(out);
System.setErr(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = br.readLine()) != null)
System.out.println(s);
out.close();
System.setOut(console);
}
}
System: setOut(PrintStream err)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;
public class MainClass {
public static void main(String[] args) {
try {
String FILE = "c:\\systemin.txt";
FileOutputStream outStr = new FileOutputStream(FILE, true);
PrintStream printStream = new PrintStream(outStr);
System.setErr(printStream);
Timestamp now = new Timestamp(System.currentTimeMillis());
System.out.println(now.toString() + ": This is text that should go to the file");
outStr.close();
printStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
System.exit(-1);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
}
System: setProperty(String key, String value)
public class Main {
public static void main(String[] args) {
System.setProperty("password", "myPassword");
System.out.println(System.getProperty("password"));
}
}