[Java Video Note]day18

Source: Internet
Author: User

Class System: Methods and properties in a class are static.

Out: Represents the standard output, which is the console by default.

In: standard input, default is keyboard.

Describe some of the system's information.

Get System Properties Information: Properties GetProperties ();

Import java.util.*;class  day18{public static void Main (string[] args) {Properties prop = system.getproperties ();// Because properties is a subclass of Hashtable, which is a subclass object in map. Then the elements in the collection can be extracted by means of a map. The collection stores all the strings, no generic definitions//How to customize some of the unique information in the system? System.setproperty ("MyKey", "myvalue");//Gets the specified property information string value = System.getproperty ("Os.name"); System.out.println (value); String v = system.getproperty ("haha"); System.out.println (v);//output null//can you load some property information dynamically when the JVM starts? In the console input JAVA-DHAHA-QQ Day18 can add some information System.out.println (v);//input then output qq//get all the property information for (Object Obj:prop.keySet ()) { String value = (string) prop.get (obj); System.out.println (obj + "..." +value);}}}

Runtime class


The class does not provide a constructor, stating that it is not possible to have a new object, and that the methods in that class are static. There are also non-static methods found in this class. Indicates that the class will certainly provide a way to get this class of objects. And the method is static, and the return value type is of this class type. It can be seen from this feature that the class uses a single-case design pattern.

The method is static Runtime getruntime ();

Import java.util.*;class  day18{public static void Main (string[] args) throws exception{runtime R = Runtime.getruntime (); Process P = r.exec ("C:\\windows\\system32\\calc.exe");//Open the Computer System program "Calculator"//process p = r.exec ("c:\\windows\\system32\\ Notepad.exe Day18.java ");//Open Notepad application and open Day18.java this file in Notepad//p.destroy ();//process kill}public static void sop (String obj) { System.out.println (obj);}}

Date class

Import Java.util.*;import java.text.*;class  day18{public static void Main (string[] args) throws exception{date D = NE W Date (); SOP (d);//output Fri 15:44:07 CST 2016//encapsulates the pattern into the SimpleDateFormat object SimpleDateFormat sdf = new SimpleDateFormat (" yyyy mm month DD day  hh:mm:ss ");//Call the Format method to format the specified Date object string time = Sdf.format (d); SOP (time);/output May 06, 2016  04:00:28}public static void sop (Object obj) {System.out.println (obj);}}

Calendar class

Import Java.util.*;import java.text.*;class  day18{public static void Main (string[] args) throws Exception{calendar c = calendar.getinstance (); String[] Mons = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};//because the West Sunday is the first day of the week, Here we use the table method to turn to our customary format string[] weeks = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int index = C.get (calendar.month); int Index1 = C.get (Calendar.day_of_week), SOP (C.get (calendar.year) + "year"),//sop ((C.get (calendar.month) + 1) + "month"), SOP (Mons [index]); SOP (C.get (Calendar.day_of_month) + "Day"),//sop ("Week" +c.get (Calendar.day_of_week)); SOP (Weeks[index1]);} public static void Sop (Object obj) {System.out.println (obj);}}

Output:

2016

May

6th

Friday


Change Date:

Import Java.util.*;import java.text.*;class  day18{public static void Main (string[] args) throws Exception{calendar c = calendar.getinstance ();//c.set (2012, 2, 23);//2 represents March, 0 represents January c.add (Calendar.year, 4);//year +4printcalendar (c);} public static void Printcalendar (Calendar c) {string[] mons = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"};//because the West Sunday is the first day of the week, here Use the table method to turn to our customary format string[] weeks = {"" "," Sunday "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "};int Index = C.get (calendar.month), int index1 = C.get (Calendar.day_of_week), SOP (C.get (calendar.year) + "year"),//sop ((C.get ( Calendar.month) + 1) + "month"), SOP (Mons[index]), SOP (C.get (Calendar.day_of_month) + "Day"),//sop ("Week" +c.get (Calendar.day_ Of_week)); SOP (Weeks[index1]);} public static void Sop (Object obj) {System.out.println (obj);}} /* Two exercises: 1. How many days to get the February of any year: set a time C.set (year, 2, 1) According to the specified years;//March 1 C.add of a certain year (Calendar.day_of_month, 1);//March 1 Push forward one day, It's the last day of February. 2. Get yesterday's present moment C.add (Calendar.day_of_month,-1); * *

Math class

Ceil () returns the smallest integer greater than the specified data

Floor () returns the largest integer less than the specified data

Round () returns the rounded integer

Pow (A, B) returns the B-side of a

Random () pseudo random number greater than or equal to 0.0 less than 1.0

Import Java.util.*;import java.text.*;class  day18{public static void Main (string[] args) throws exception{for (int x = 0; x < 10; X + +) {//double d = math.random (),//0 to 1int d = (int) (Math.random () * 10 + 1);//1 to 10 random integer containing 10sop (d);} Another way to do this is random r = new Random (), for (int x =0; x < x + +) {int d = r.nextint (10) + 1;//1 to 10 for a randomized integer containing 10sop (d);}} public static void base () {Double d = math.ceil (3.6); SOP (d);//Output 4.0d = Math.ceil ( -3.6); SOP (d);//Output -3.0d = Math.floor (3.6) SOP (d);//Output 3.0d = Math.floor ( -3.6); SOP (d);//output -4.0long L = Math.Round (12.34); SOP (L);//Output 12l = Math.Round (12.54); SOP ( l);//Output 13sop (Math.pow (2, 3));//output 8.0}public static void sop (Object obj) {System.out.println (obj);}}

IO (Input Output) stream

1. IO stream is used to process data transfer between devices

2. Java operations on data are streamed through the way

3. The objects that Java uses to manipulate the flow are in the IO package

4. Streaming is divided into two types of data: byte stream and character stream.

5. Flow is divided into: input stream, output stream.

Abstract base class for byte stream

InputStream OutputStream

Abstract base class for character streams

Reader Writer

The subclass name derived from these four classes is the suffix of its parent class as the name of the subclass.

such as: InputStream sub-class FileInputStream

such as: Reader Subclass FileReader

Learn the character stream character first.

Now that the IO stream is used to manipulate the data. Then the most common form of data is: files. First, the operation of the main file to demonstrate.

Requirement: On the hard disk, create a file and write some text data.


Found a writer subclass object specifically for manipulating files, FileWriter. The suffix name is the parent class name, and the prefix name is the function of the stream object.

Import java.io.*;class  day18{public static void Main (string[] args) throws ioexception{//creates a FileWriter object, Once the object is initialized, it must be explicitly//manipulated, and the file will be created in the specified directory. If the directory//already has a file with the same name, it will be overwritten. In fact, the step is to clear the location of data storage. FileWriter FW = new FileWriter ("Demo.txt");//Call the Write method to write the string to the stream Fw.write ("ABCDE");//Refresh the data in the buffer in the Stream object, Brushes the data to the destination Fw.flush ();//Closes the stream resource, but flushes the data inside the buffer one time//brushes the data to the destination//and flush the Difference: flush () refresh the stream can continue to use, close after refresh// Turn the stream off Fw.close ();}}

How the IO exception is handled.

Import java.io.*;class  day18{public static void Main (string[] args) {FileWriter fw = NULL;TRY{FW = new FileWriter ("Dem O.txt "); Fw.write (" ABDFD ");} catch (IOException e) {System.out.println (e.tostring ());} finally//must be closed inside, and determine whether the Fw establishes object {try{if (fw! = null) Fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}}}

Continue to write data on existing documents.

Import java.io.*;class  day18{public static void Main (string[] args) throws ioexception{//pass a true parameter, which means that the existing file is not overwritten. And the continuation of the data in the//at the end of the existing document. FileWriter FW = new FileWriter ("Demo.txt", true); Fw.write ("haha"); Fw.write ("Nihao\r\nxiexie");/\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ In Windows /in Linux only write \ n can fw.close ();}}


Text file read Way one:

Import java.io.*;class  day18{public static void Main (string[] args) throws ioexception{//create a file read stream object, associated with the file with the specified name /To ensure that the file is already present, if it does not exist, an exception occurs filenotfoundexceptionfilereader fr  = new FileReader ("Demo.txt");//Call the Read method that reads the Stream object// The Read () method reads one character at a time and automatically reads it down. int ch = fr.read (), SOP ((char) ch), ch = fr.read (), SOP ((char) ch), fr.close (), ch = fr.read (),//If equals-1, then read/*while (True) { int ch = fr.read (); if (ch = =-1) break;sop ((char) ch);} */int ch = 0;while ((ch = fr.read ())! =-1) {SOP ((char) ch);}} public static void Sop (Object obj) {System.out.println (obj);}}

Text file read mode two:

Read through a character array.

Import java.io.*;class  day18{public static void Main (string[] args) throws Ioexception{filereader fr  = new FileReader ("Demo.txt");//defines a character array to store the read characters. This read (char[]) method returns the number of characters read char[] buf = new Char[1024];int num = 0;while ((num =fr.read (BUF))! =-1) {SOP (New String (buf, 0 , num));//start printing from 0, and print num}fr.close () Every time you read out num;} public static void Sop (Object obj) {System.out.println (obj);}}

Practice:

Read A. java file and print it on the console.

Import java.io.*;class  day18{public static void Main (string[] args) throws Ioexception{filereader fr  = new FileReader ("Demo.txt"); char[] buf = new Char[1024];int num = 0;while ((num =fr.read (BUF))! =-1) {System.out.print (new Stri Ng (buf, 0, num));} Fr.close ();}}

Copy a text file

Copy the C-drive text file to the D drive.

The principle of replication:

The fact is that the data under the C drive is stored in a file on the D drive.

Steps:

1. Create a file in the D drive to store the data in the file.

2. Define read-stream and C-drive file associations.

3. Complete data storage through continuous reading and writing.

4. Close Resources

Import java.io.*;class  day18{public static void Main (string[] args) throws ioexception{copy_2 (); public static void Copy_2 () {FileWriter FW = null; FileReader FR = NULL;TRY{FW = new FileWriter ("Demo_copy2.txt"), FR = new FileReader ("Demo.txt"); char[] buf = new char[1024] ; int len = 0;while (len = Fr.read (BUF))! =-1) {fw.write (buf, 0, Len);}} catch (IOException e) {throw new RuntimeException ("Read and Write Failed");} FINALLY{IF (fw! = null) Try{fw.close ();} catch (IOException e) {}if (fr! = null) Try{fr.close ();} catch (IOException e) {}}}//reads a character from the C disk, writes a character to the D disk, public static void Copy_1 () throws ioexception{//creates the destination filewriter FW = new Fi Lewriter ("Demo_copy.txt");//associated with an existing file FileReader fr = new FileReader ("Demo.txt"); int ch = 0;while ((ch = fr.read ())! =-1) {FW . write (ch);} Fw.close (); Fr.close ();}}






[Java Video Note]day18

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.