Java基礎總結(三)

1、開始的嘮叨前端

Java中有一大塊的內容是組件及事件處理,一刷Java時是爲了寫Web,前端有Html沒學這部份內容。二刷時已經在作Android了,前端有自帶的組件,依然跳過。編程

我也不建議道友去學習這部份內容,如今真的有人用Java寫C/S嗎?windows

、學習筆記設計模式

(一)輸入輸出流數組

1.流:輸入流、輸出流是針對程序而言的。服務器

全部輸入流都是InputStream(字節輸入流)或Reader(字符輸入流)的子類;網絡

全部輸出流都是OutputStream(字節輸出流)或Writer(字符輸出流)的子類;(漢字佔2個字節,對中文而言,字節流有可能出現亂碼)多線程

2.File類:ide

File類用於對文件的操做,有三種構造方法函數

File(String filename);
File(String directoryPath,String filename);
File(File dir,String filename);

File類的方法十分簡單,全是對文件的一些操做,顧名思義便可。

很少見的是File能夠用來運行可執行文件,如下方法能夠打開Windows的記事本:

public static void main(String[] args) {
    File note=new File("c:/windows","Notepad.exe");
    Runtime rt=Runtime.getRuntime();
    try {
        rt.exec(note.getAbsolutePath());    
    } catch (IOException e) {
        e.printStackTrace();
        }
    }

 

2.字節輸入流與字節輸出流:

輸入流基本步驟:

  • 設定輸入流的
  • 建立指向源的輸入流
  • 讓輸入流讀取源中的數據
  • 關閉輸入流

輸出流基本步驟:

  • 給出輸出流的目的地
  • 建立指向目的地的輸出流
  • 讓輸出流把數據寫入到目的地
  • 關閉輸出流

下面的例子運用IO完成了文本的複製

    public static void main(String[] args) {
        try {
        
            FileInputStream in=new FileInputStream(new File("d:\\a.txt"));
            FileOutputStream out=new FileOutputStream(new File("d:\\b.txt"));
            int len=0;
            byte [] b=new byte[1024];
            try {
                while((len=in.read(b))!=-1){
                    out.write(b, 0, len);
//                    out.write(b);
                }            
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }                    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

void write(byte b[])方法,向目的地寫入一個字節數組b;

void write(byte b[],int off,int len)方法,從給定字節數組b中起始於偏移量off處取len個字節寫到目的地。

輸入流的read方法也有這兩種結構。

3.字符輸入流與字符輸出流:

文件字符流:FileReader、FileWriter

緩衝流:BufferedReader、BufferedWriter:更高級的流,讀寫功能更強。涉及到Java設計模式的裝飾者模式,不屬於基礎範圍。

4.序列化:  實現Serializable接口的對象是序列化對象,序列化對象能夠寫入或者讀入到流中。

Serializable接口中的方法對程序是不可見的,所以實現該接口的類不須要實現額外的方法。

(二)多線程機制

 1.線程的狀態與生命週期:

  • 新建:Thred或其子類聲明一個對象。
  • 運行:線程調用strat()方法,在JVM中排隊等候,輪到線程享用CPU資源時,調用run()方法。
  • 中斷:4種緣由。JVM將CPU資源輪給其餘線程;線程調用sleep()方法;線程調用wait()方法;線程阻塞。
  • 死亡:2種緣由。線程完成了任務;線程被強制中斷。

2.線程的優先級:線程優先級能夠經過setPriority(int grade)方法調整,默認優先級爲5。通常不提倡更改優先級。

3.Thread與Runable:

建立線程目標對象時必須向構造方法中傳入一個實現Runable接口類的實例。這樣一旦輪到線程享用CPU資源,就會經過接口回調調用run()方法。

咱們知道,線程能夠共享進程的資源,所以對於使用同一目標對象(Runable)的線程,目標對象的成員變量天然就是這些線程共享的數據單元。

下例中,首先建立一個實現Runable接口的House類,用於提供數據單元,而且在構造方法中將本身做爲參數傳遞到Thread中。

public class House implements Runnable{
private int waterAmount;
public int getWaterAmount() {
    return waterAmount;
}
public void setWaterAmount(int waterAmount) {
    this.waterAmount = waterAmount;
}
public Thread cat,dog;
public House(){
    cat=new Thread(this);
    dog=new Thread(this);
}
    @Override
    public void run() {
        while(true){
            Thread thread=Thread.currentThread();
            if(thread==dog){
                System.out.println("dog"+waterAmount--);
            }else {
                System.out.println("cat"+waterAmount--);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(waterAmount<=0){
                return;                                   
            }
        }    
    }
}

主函數中,分別開啓dog與cat線程。不一樣的電腦中結果不一樣,這也是多線程的一個特色。

public static void main(String[] args) {
        House house=new House();
        house.setWaterAmount(5);                                                                  
        house.dog.start();
        house.cat.start();
    } 

4.線程同步:使用synchronized修飾同步方法,確保當前只有一個線程能夠調用該方法。

5.協調線程同步:當一個線程使用同步方法中用到某個變量,而該變量須要其餘線程修改後才符合要求時,就要利用wait()和notify()、notifyAll()手動協調線程同步。

(三)網絡編程

1.URL:包括最基本的三部分信息,協議、地址、資源

2.InetAddress:獲取域名和IP地址

public static void main(String[] args) {
        InetAddress address = null;
        try {
            address = InetAddress.getByName("www.jzme.cn");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(address);
        try {
            InetAddress local=InetAddress.getLocalHost();
            System.out.println(local);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        
    }

 

【運行結果】

3.網絡編程:網絡編程通常是集大成者,涉及到IO、網絡、線程等多個方面。下例能夠獲取用戶輸入的網址的回調的信息。

網絡訪問時耗時操做,須要在子線程中進行

public class Look implements Runnable{
private URL url;
    public void setUrl(URL url) {
    this.url = url;
}
    @Override
    public void run() {
        String s = null;
        try {
            InputStream in=url.openStream();    
            FileOutputStream out=new FileOutputStream(new File("d:\\c.txt"));
            int len=0;
            byte[] b=new byte[1024];
            while((len=in.read(b))!=-1){
                out.write(b, 0, len);
//                out.write(b);
            }            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    } 

主函數接受用戶輸入,生成URL並傳遞給Look類的實例,Look類的實例打開輸入流獲取數據,並經過輸出流將數據寫入本地文本

public static void main(String[] args) {
        System.out.println("輸入網站地址:");
        Scanner scanner=new Scanner(System.in);
        URL url = null;
        try {
            url = new URL(scanner.nextLine());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Look look=new Look();
        look.setUrl(url);
        Thread thread=new Thread(look);
        thread.start();
    }

 

 當輸入的地址爲「www.baidu.com」時,c.txt文本部份內容以下:

 

【如下涉及到服務器的部分知識,我的感受不屬於基礎範圍,僅列出做爲補充】

4.TCP協議-Socket

5.UDP協議-DatagramSocket

 三、結束的嘮叨

Java基礎的整理到此爲止。從複習到完成博客,斷斷續續花了5天左右。參考的書籍是耿祥義老師的教材。

接下來打算分兩步走,一邊梳理Android的知識點,寫點小工具,一邊啃幾本經典的書籍。

最後奉勸你們,學海無涯,懸崖勒馬。

相關文章
相關標籤/搜索