Java Common entity class--system class

Source: Internet
Author: User
Tags tortoisesvn lenovo

strings, dates, and numbers are the most commonly used data objects in Java programs, and the creation, modification, formatting, and transformation of these data are incorporated into every corner of the Java program and must be mastered. This section demonstrates the following common entity classes through an instance

    • Java System-Level classes: System class systems, runtime Class runtime

    • Java String processing classes: String classes, String delimiter classes StringTokenizer, Thread-safe mutable string classes stringbuffer, variable string classes StringBuilder

    • Java Date-Processing classes: Date class dates, date formatting classes dateformate, Calendar class calendars

    • Java Digital Processing class: Math class Maths, Random class Radom, math class float, Double, Integer, Long


1. Java System Level Class

System class Systems: Access System Properties, access environment variables, load files and libraries, quickly copy arrays, get system time, System eject commands, perform garbage collection

Runtime class Runtime: View system memory, terminate JVM virtual machine, run System program, use close hook


1.1 Accessing System Properties

For example, get the operating system name Os.name, operating system version os.version, JVM name Java.vm.name, JDK version java.version, and so on. The System Properties collection is shown in table 1-1

Table 1-1 system Property key values


Java.version Java Runtime Environment version
Java.vendor Java Runtime Vendor
Java.home Java installation directory
Java.vm.version Java Virtual Machine version
Java.vm.name Java Virtual machine Name
Java.class.version Java Class format version number
Java.class.path Java class path
Os.name Name of the operating system
Os.version Version of the operating system
File.separator File delimiter
Path.separator Path delimiter
Line.separator Row delimiter
User.dir User's current working directory
There are two ways to access system properties

(1), get the list of all system attributes

Use the System.getproperties () function to get a properties object that contains key-value pairs for all system properties. It then translates to an iterator enumeration the amount of the object, and then uses the while () loop to display the output of all keys and value.

Package Org.test.envm;import Java.util.enumeration;import Java.util.properties;public class SYSTEMENVM {public static void Main (string[] args) {//Get System attribute List Properties Properties = System.getproperties (); Enumeration<object> e = Properties.keys (); while (E.hasmoreelements ()) {string key = (string) e.nextelement (); String value = Properties.getproperty (key); System.out.println (key+ "=" +value);}}

The results appear as follows:

Java.runtime.name = java (TM)  SE Runtime Environmentsun.boot.library.path =  c:\program files\java\jre6\binjava.vm.version = 20.14-b01java.vm.vendor = sun  Microsystems inc.java.vendor.url = http://java.sun.com/path.separator = ;java.vm.name  = java hotspot (TM)  64-Bit Server VMfile.encoding.pkg =  sun.iosun.java.launcher = sun_standarduser.country = cnsun.os.patch.level =  service pack 1java.vm.specification.name = java virtual machine  specificationuser.dir = d:\workspace\new1_workspace\threadjava.runtime.version = 1.6.0_39- B04java.awt.graphicsenv = sun.awt.win32graphicsenvironmentjava.endorsed.dirs = c:\program  files\java\jre6\lib\endorsedos.arch = amd64java.io.tmpdir = c:\users\admini~1\ Appdata\local\templine.separator = java.vm.specification.vendor = sun microsystems inc.user.variant = os.name =  windows 7sun.jnu.encoding = gbkjava.library.path = c:\program files\java\ Jre6\bin; C:\Windows\Sun\Java\bin; C:\Windows\system32; C:\Windows; C:\oraclexe\app\oracle\product\11.2.0\server\bin; C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\; c:\program files\java\jdk1.7.0_71\bin; c:\program files\java\jdk1.7.0_71\jre\bin;d:\soft\apache-maven-3.0.5\bin; c:\program files\tortoisesvn\bin; c:\program files\lenovo\fingerprint manager pro\; c:\program files\tortoisesvn\bin; c:\program files  (x86) \subversion\bin;d:\soft\apache-tomcat-7.0.56\bin;. java.specification.name = java platform api specificationjava.class.version =  50.0sun.management.compiler = hotspot 64-bit tiered compilersos.version =  6.1user.home = c:\users\administratoruser.timezone = java.awt.printerjob =  sun.awt.windows.wprinterjobfile.encoding = gbkjava.specification.version =  1.6java.class.path = d:\workspace\new1_workspace\thread\binuser.name =  administratorjava.vm.specification.version = 1.0sun.java.command =  org.test.envm.systemenvmjava.home = c:\program files\java\jre6sun.arch.data.model =  64user.language = zhjava.specification.vendor = sun microsystems inc.awt.toolkit  = sun.awt.windows.wtoolkitjava.vm.info = mixed modejava.version = 1.6.0_ 39java.ext.dirs = c:\program files\java\jre6\lib\ext; c:\windows\sun\java\lib\extsun.boot.class.path = c:\program files\java\jre6\lib\resources.jar; c:\program files\java\jre6\lib\rt.jar; c:\program files\java\jre6\lib\sunrsasign.jar; C:\program files\java\jrE6\lib\jsse.jar; c:\program files\java\jre6\lib\jce.jar; c:\program files\java\jre6\lib\charsets.jar; c:\program files\java\jre6\lib\modules\jdk.boot.jar; C:\program files\java\jre6\classesjava.vendor = sun microsystems inc.file.separator  = java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgisun.io.unicode.encoding  = unicodelittlesun.cpu.endian = littlesun.desktop = windowssun.cpu.isalist  = amd64

(2), get the value of a system property

If we know the key of the property to query, you can also directly use System.getproperties (key) to get the value of the key directly.

TODO Gets a System attribute value of String osname = System.getproperty ("Os.name");//Get os.name String osversion = System.getproperty ("O    S.version ");//Get os.version String filesep = System.getproperty (" File.separator ");    SYSTEM.OUT.PRINTLN ("Operating system name:" +osname);    SYSTEM.OUT.PRINTLN ("OS version:" +osversion); System.out.println ("File delimiter:" +filesep);

The results appear as follows:

Operating system name: Windows 7 Operating system version: 6.1 File delimiter: \


1.2. Accessing Environment variables

System properties are intrinsic to the system and cannot be modified. The environment variable is modifiable. Environment variables include system variables and user variables.

Use system to get environment variables.

(1) Get a list of all environment variables

Use the system.getenv () function to get a map object that contains key-value pairs for all environment variables. Use this function to remove the object list map, and then iterate, using the while () loop output

Get the list of environment variables map<string, string> getenv = system.getenv ();iterator<string> Iterator = Getenv.keyset (). Iterator (); while (Iterator.hasnext ()) {String key = Iterator.next (); SYSTEM.OUT.PRINTLN (key + "=" + getenv.get (key));}

The results are as follows:

userprofile = c:\users\administratorjava_home = c:\program files\java\jdk1.7.0_ 71tomcat_classpath = ;D: \ Soft\apache-tomcat-7.0.56\lib\servlet-api.jarsystemdrive = c: #envTSLOGsss2796  =  75890384path = c:\oraclexe\app\oracle\product\11.2.0\server\bin; C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\; c:\program files\java\jdk1.7.0_71\bin; c:\program files\java\jdk1.7.0_71\jre\bin;d:\soft\apache-maven-3.0.5\bin; c:\program files\tortoisesvn\bin; c:\program files\lenovo\fingerprint manager pro\; c:\program files\tortoisesvn\bin; c:\program files  (x86) \subversion\bin;d:\soft\apache-tomcat-7.0.56\bin#envtslogxmedialibrary2796  = 178637744processor_revision = 3c03userdomain = win-ro8hm9vf60iallusersprofile  = c:\programdatacatalina_base = d:\soft\apache-tomcat-7.0.56sessionname = consoletmp = c:\users\admini~1\appdata\local\temp#envkkprbc_cmdilne = logonserver =  \\win-ro8hm9vf60i=:: = ::commonprogramfiles = c:\program files\common  Filesprocessor_level = 6localappdata = c:\users\administrator\appdata\localcomputername  = win-ro8hm9vf60isystemroot = c:\windowsusername = administratornls_lang =  simplified chinese_china. Zhs16gbktomcat_home = d:\soft\apache-tomcat-7.0.56appdata = c:\users\administrator\appdata \roamingprogramdata = c:\programdatapathext = .com;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH;. Mscwindows_tracing_logfile = c:\bvtbin\tests\installpackage\csilogfile.logmaven_home = d:\ Soft\apache-maven-3.0.5programfiles (x86)  = C:\Program Files  (x86) windows_tracing_flags  = 3temp = c:\users\admini~1\appdata\local\tempprogramfiles = c:\Program fileshomedrive = c:tns_admin = c:\oraclexe\app\oracle\product\11.2.0\server\ network\adminprogramw6432 = c:\program filesprocessor_identifier = intel64  family 6 model 60 stepping 3, genuineintelshelllaunch{ a81ba54b-ccfe-4204-8e79-a68c0fdfa5cf} = shellextclasspath = .; c:\program files\java\jdk1.7.0_71\lib; C:\program files\java\jdk1.7.0_71\lib\tools.jarprocessor_architecture = amd64fp_no_host_check  = noos = windows_nthomepath = \users\administratorjre_home = c:\ Program files\java\jre7#envtslogrbcshellext2796 = 2178528commonprogramw6432 = c:\ program files\common filesapr_iconv_path = c:\program files  (x86) \Subversion\ Iconvcatalina_home = d:\soft\apache-tomcat-7.0.56windir = c:\windowsnumber_of_processors  = 4psmodulepath = c:\wIndows\system32\windowspowershell\v1.0\modulespublic = c:\users\publiccommonprogramfiles (x86)  =  C:\Program Files  (x86) \common filescomspec = c:\windows\system32\cmd.exe

To get a variable value, call System.getenv (key) directly.

1.3. loading files and libraries

    • The function load (String filename) is to load a code file from the local file system as a dynamic library with the specified file name, and the file name parameter must be the full path.

1.4 Exit System Command Runtime.getruntime (). exit (n);

1.5 garbage collection: System.GC ()

1.6 Acquisition System time: System.currenttimemillis ()

1.7 Quick Copy array: void Arraycopy (Object src,int srcpos,object dest,int destpos,int length)

This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1773913

Java Common entity class--system class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.