<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 14,  comments - 7,  trackbacks - 0
      2010年10月9日
           

            剛學java流技術(shù)IO編程時,一下出來這么多流,很是不敏感,不感冒,在學習了這段時間,發(fā)現(xiàn)好多項目中的讀寫數(shù)據(jù),輸入輸出用到的都是以前學的最基本的,但是自己卻不是很理解其根本,今天就說幾個流中簡單但又常用的幾個知識點

       Java.io包中定義了多個流類型,來實現(xiàn)輸入輸出功能:

      •  按數(shù)據(jù)流的方向不同可以分為輸入流和輸出流。(以程序的角度來考慮)
      • 如:輸入流InputStream Reader 輸出流 OutputStream Writer
      •  按處理數(shù)據(jù)單位不同可以分為字節(jié)流和字符流。
      • : 字節(jié)流 InputStream OutputStream 字符流 Reader Writer
      •  按照功能不同可以分為節(jié)點流和處理流。

            通俗的說節(jié)點流就是一個簡單的輸入輸出流,而處理流是“連接”在已存在的字節(jié)流或處理流之上的,通過對數(shù)據(jù)的處理為程序提供更為強大的讀寫功能。

     我們最經(jīng)常用到的節(jié)點流類型是有關(guān)文件處理的有FileReader FileWriter FileInputStream FileOutputStream

    Ø          FileInputSteam FileOutputStream 類支持其父類InputStream OutputStream 所提供的數(shù)據(jù)讀寫方法。

    Ø       注意:

    §         在實例化FileInputStreamFileOutputSteam流時要用trycatch語句以處理其可能拋出的FileNotFoundException

    §         在讀寫數(shù)據(jù)時也要用trycatch語句以處理可能拋出的 IOException

    §         FileNotFoundExceptionIOException的子類

    FileReader FileWriter 分別繼承自ReaderWriterFileInputSteamFileOutputStream類似,所不同的時FileReaderFileWriter向文件輸入和輸出的數(shù)據(jù)單位為字符。

         處理流中常用到的有緩沖流 、轉(zhuǎn)換流。

    l          緩沖流有BufferedReader BufferedWriter BufferedInputStream BufferedOutputStream ,緩沖流要“套接”在相應的節(jié)點流之上,對讀寫的數(shù)據(jù)提供了緩沖的功能,提高了讀寫的效率,同時增加了一些新的方法

    l          BufferedReader提供了readLine方法用于讀取一行字符串(以"r"n           隔)。

    l          BufferedWriter提供了newLine用于寫入一個行分隔符。

    l          對于輸出的緩沖流,寫出的數(shù)據(jù)會先在內(nèi)存中緩存,使用flush方法將會使內(nèi)存中的數(shù)據(jù)立刻寫出。

    Socket編程中接受信息時,不是直接用InputStream接收,而是把內(nèi)容放入緩沖之中進行讀取 ,在Socket中的簡單應用如下
      

    package com.dr.Echo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class EchoServer {

        
    /**
         * 
    @param args
         * 
    @throws IOException 
         
    */

        
    public static void main(String[] args) throws IOException {
            
    // TODO Auto-generated method stub
             ServerSocket server = null;
             PrintStream out 
    = null;
             BufferedReader buf 
    = null;
             server 
    = new ServerSocket(9999);
             Socket client 
    = null;
             
    while(true){
                 
    //不斷接收數(shù)據(jù)
                 client = server.accept();
                 
    //準備好像客戶端輸出內(nèi)容
                 out = new PrintStream(client.getOutputStream());
                 
    //而且客戶端要有輸入給服務器端
                 buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
                 
    //下面先給出一個完整的信息提示
                 out.println("您好!歡迎光臨:http://wwww.tstc.edu.cn");
                 out.println(
    "輸入bye表示退出哈-");
                 
    while(true){
                     
    //接受客戶端發(fā)送而來的內(nèi)容
                     String str = buf.readLine();
                     System.out.println(
    "client's data is "+str);
                     
    if(str == null){
                         
    //如果str為空就表示退出
                         break;
                     }
    else{
                         
    //如果輸入的是bye則表示是退出系統(tǒng)
                         if("bye".equals(str)){
                             
    break;
                         }

                         out.println(
    "xiaoxiao:"+str);
                     }

                 }

                 out.close();
                 buf.close();
                 client.close();
             }

        }


    }

     

    package com.dr.Echo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class EchoClient {

        
    /**
         * 
    @param args
         * 
    @throws IOException 
         * 
    @throws UnknownHostException 
         
    */

        
    public static void main(String[] args) throws UnknownHostException, IOException {
            
    // TODO Auto-generated method stub
             Socket client = null;
             BufferedReader buf 
    = null;
             PrintStream out 
    = null;
             client 
    = new Socket("127.0.0.1",9999);
             buf 
    = new BufferedReader(new InputStreamReader(client.getInputStream()));
             System.out.println(buf.readLine());
             System.out.println(buf.readLine());
             BufferedReader in 
    = new BufferedReader(new InputStreamReader(System.in));
             
             String userInput 
    = null;
             out 
    = new PrintStream(client.getOutputStream());
             
    while((userInput = in.readLine())!=null){
                 out.println(userInput);
                 System.out.println(buf.readLine());
             }

             out.close();
             in.close();
             client.close();
             
             
        }


    }

       

          轉(zhuǎn)換流InputStreamReader OutputStreamWriter 用于字節(jié)數(shù)據(jù)到字符數(shù)據(jù)之間的轉(zhuǎn)換。其中InputStreamReader 需要和InputStream”套接OutputStreamWriter需要和OutputStream套接

          數(shù)據(jù)流DataInputStream DataOutputStream 提供了可以存取與機器無關(guān)的java原始數(shù)據(jù)類型

    l              Print流:PrintWriterPrintStream都屬于輸出流,分別針對與字符和字節(jié),PrintWriterPrintStream提供了重載的printPrintln方法用于多種數(shù)據(jù)類型的輸出。PrintWriterPrintStream的輸出操作不會拋出異常,用戶通過檢測錯誤狀態(tài)獲取錯誤信息。PrintWriterPrintStream有自動flush功能

    l           

     這幾個流的類型都是在以后的學習中經(jīng)常用到的。就不一一做例子啦

     

             以后所學到的所有流類型都是繼承 InputStream OutputStream Reader Writer 這四個抽象的流類型

    posted @ 2011-01-18 22:26 迷人笑笑 閱讀(880) | 評論 (0)編輯 收藏
         摘要:           我相信大多說人都對構(gòu)造方法、方法不陌生,而且很了解,但我也相信有很多像我這樣的沒有一個很好很清晰很全面的認識,今天就把它整理如下,希望能給大家?guī)睃c方便與幫助,也希望大家多多討論。         ...  閱讀全文
    posted @ 2010-11-12 16:43 迷人笑笑 閱讀(39161) | 評論 (1)編輯 收藏
         摘要:        今天我們用實例程序說一下java中常見的生產(chǎn)者與消費者的問題。  首先看一下里面線程間的通信問題:    一個線程向數(shù)據(jù)存儲空間添加數(shù)據(jù)(生產(chǎn)者),另一個線程從數(shù)據(jù)存儲空間去出數(shù)據(jù)(消費者)。 這個程序中有兩個可能出現(xiàn)的問題需要考慮: 1、   ...  閱讀全文
    posted @ 2010-11-10 23:07 迷人笑笑 閱讀(2157) | 評論 (1)編輯 收藏
         摘要: 項目需求:1.病人來掛號時根據(jù)自己想找的醫(yī)生掛相應的醫(yī)生的號,即加到相應醫(yī)生的病人隊列中。                    2.醫(yī)生看見自己的屏幕,點擊自己的名字,得到自己相應列表的下一位病人 具體代碼如下: ...  閱讀全文
    posted @ 2010-11-02 16:51 迷人笑笑 閱讀(3464) | 評論 (1)編輯 收藏
         摘要: 1.當我們?nèi)ャy行辦理什么業(yè)務的時候總會先在一個類似電腦的小系統(tǒng)上去一個號,然后等著,當被叫到時就去相應的窗口辦理業(yè)務,這種排隊的小系統(tǒng)主要用到了接口Queue,因為它符合隊列的要求先進先出。 2.還有就是有種特殊情況就是,銀行的金卡用戶,他們的權(quán)限較高,總可以在普通用戶的前面辦理業(yè)務 相應代碼如下: package com.dr.queue; public clas...  閱讀全文
    posted @ 2010-11-01 13:22 迷人笑笑 閱讀(3767) | 評論 (0)編輯 收藏
    代碼如下:
    package com.dr.exercise6;

    import java.util.Scanner;

    public class Login {
        
    public static void main(String[] args) {
            Scanner s 
    = new Scanner(System.in);
            System.out.println(
    "請輸入用戶名:");
            String name 
    = s.nextLine();
            System.out.println(
    "請輸入密碼:");
            String password 
    = s.nextLine();
            
    if(name.equals("abc")&&password.equals("123")){
                System.out.println(
    "歡迎登入!!");
                
            }

            
    else{
                System.out.println(
    "用戶名或密碼不正確!!");
            }


        }


    }

    用戶名密碼輸入正確時:
    用戶名或密碼輸入不正確時:
    posted @ 2010-10-20 16:33 迷人笑笑 閱讀(621) | 評論 (0)編輯 收藏
    答案是兩個,現(xiàn)在我們具體的說一下:
    String s = new String("abc");
    首先我們要明白兩個概念,引用變量和對象,對象一般通過new在堆中創(chuàng)建,s只是一個引用變量。
    所有的字符串都是String對象,由于字符串文字的大量使用,java中為了節(jié)省時間,在編譯階段,會把字符串文字放在文字池中,文字池的一個好處就是可以把相同的字符串合并,占用一個空間,我們可以用==判斷一下兩個引用變量是否指向了一個地址即一個對象
    public class Demo02 {
        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            String s1 ="abc";
            String s2 
    = "abc" ;
            
    if( s1 == s2 ) System.out.println("s1,s2 refer to the same object");
            
    else System.out.println("trouble");
        }

    }
    輸出結(jié)果為:
    可以看出指向了一個對象,即文字池中保存了一個對象。
    二、String s = new String("abc")實際上是"abc"本身就是文字池中的一個對象,在運行 new String()時,把文字池即pool中的字符串"abc"復制到堆中,并把這個對象的應用交給s,所以創(chuàng)建了兩個String對象,一個在pool中,一個在堆中。下面看這個程序,創(chuàng)建了幾個對象。
    public class Demo03 {
        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            String s1 = new String("abc") ;
            String s2 
    = new String("abc") ;
            
    if( s1 == s2 )//不會執(zhí)行的語句}
           System.out.println("在堆中創(chuàng)建了一個對象");

        }

            
    else{
            System.out.println(
    "在堆中創(chuàng)建了兩個對象");    
            }

            }

    }
    輸出結(jié)果為:
    可知在堆中創(chuàng)建了兩個對象,但是在文字池中有一個對象,所以共創(chuàng)建了三個對象。
    三、再看下一個例子:
    public class Demo04 {
        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            String s = new String("abc");
            s 
    = "cba";
            System.out.println(s); 
        }


    }
    輸出結(jié)果為:
    String s = new String("abc")并不是給s賦值,而是把字符串"abc"的引用交給s持有,現(xiàn)在把s指向字符串"cba","cba"的引用地址把"abc"的引用地址覆蓋,所以輸出結(jié)果為cba

    最后這個輸出結(jié)果的解釋,希望大家指出不足!!

    posted @ 2010-10-18 14:32 迷人笑笑 閱讀(10653) | 評論 (0)編輯 收藏
    要求為:寫一個公司員工類;
                    數(shù)據(jù)成員:員工號,姓名,薪水,部門;
                    方法:
                    利用構(gòu)造方法完成設置信息:
                     A.單參,只傳遞員工號;
                     B.雙參,只傳遞員工號、姓名;
                     C.四參,傳遞員工號、姓名、薪水、部門;
                     D.無參,則均為空值;
    main方法中創(chuàng)建兩個對象,分別進行驗證。
    package com.dr.computer;

    public class Worker {
         
    private String id;
         
    private String name;
         
    private float salary;
         
    private String department;
         
    public Worker(String n){
           
    this.setId(n);
           
    this.setName("無名氏");
           
    this.setSalary(0.0f);
           
    this.setDepartment("未定");
         }

         
    public Worker(String n,String na){
             
    this.setId(n);
             
    this.setName(na);
             
    this.setSalary(1000);
             
    this.setDepartment("后勤");
         }

         
    public Worker (String n,String na,float sa,String dep){
             
    this.setId(n);
             
    this.setName(na);
             
    this.setSalary(sa);
             
    this.setDepartment(dep);
         }

         
    public Worker(){
             
         }

        
    public String getId() {
            
    return id;
        }

        
    public void setId(String n) {
            
    this.id = n;
        }

        
    public String getName() {
            
    return name;
        }

        
    public void setName(String name) {
            
    this.name = name;
        }

        
    public float getSalary() {
            
    return salary;
        }

        
    public void setSalary(float f) {
            
    if(f>0.0f){
            
    this.salary = f;
            }

        }

        
    public String getDepartment() {
            
    return department;
        }

        
    public void setDepartment(String department) {
            
    this.department = department;
        }

        
    public void showWorker(){
            System.out.println(
    "員工信息為:");
            System.out.println(
    "\t+--我的Id號:"+id);
            System.out.println(
    "\t+--我的名字:"+name);
            System.out.println(
    "\t+--我的工資:"+salary);
            System.out.println(
    "\t+--我的部門:"+department);
        }

    }

    package com.dr.computer;

    public class WorkerTest {

        
        
    public static void main(String[] args) {
            Worker w1 
    =new Worker("0001","泉水");
            Worker w2 
    =new Worker("0002","陽光",1000.0f,"公關(guān)部");
            w1.showWorker();
            w2.showWorker();
        }

        
        }




        
    posted @ 2010-10-15 22:47 迷人笑笑 閱讀(437) | 評論 (0)編輯 收藏

     

    類是一個抽象的概念,對象則是類的具體實例,是真實的個體,比如人就是一個類,張三、李四等就是一個個真實的

    具體的個體對象,人有具備所有人類通用的特征,但不能確定是哪個人,張三、李四等有具體的特征,比如身高性別,即

    屬性。類一種看不見摸不著的東西,對象是“有血有肉”的實物。
          現(xiàn)實生活中到處皆對象,比如一輛汽車,一棟房子,一臺電腦,一只貓。
          面向?qū)ο缶哂蟹庋b、繼承、多態(tài)三大特征。封裝,即使屬性用private修飾對外部不可見;繼承,如人,有老人、小孩

    、男人、女人,他們都是人的子類。多態(tài)包括方法的重載和對象的多態(tài)性,方法的重載就是方法名相同,但參數(shù)和類型不

    同的方法,多態(tài),即人分為老人,小孩,男人,女人等。

    posted @ 2010-10-15 15:13 迷人笑笑 閱讀(809) | 評論 (0)編輯 收藏
              java采用堆棧形式對數(shù)據(jù)進行存取,這樣做的優(yōu)點是存取速度較快,因為棧的速度比較快,僅次于寄存器的速度,堆棧的模式符合垃圾回收的機制,有利于垃圾的回收,垃圾收集器(GC)自動回收。
              現(xiàn)在用一個小實例來感受一下java的堆棧存取,代碼如下:
    public class Person {
        String  name;
        
    int  age;
        
    public static void main(String[] args) {
            Person p1 
    = new Person();
            Person p2 
    = null;
            
            p2 
    = p1;
            p1.name 
    = "張三";
            p1.age  
    = 23;
            p2.name 
    = "李四";
            p2.age 
    = 24;
            
            p1.say();
            p2.say();
            
        }

        
    void say(){
            System.out.println(
    "我的名字是:"+ name +"年齡 :"+age);
        }
    運行結(jié)果如下:我的名字是:李四年齡 :24
                                我的名字是:李四年齡 :24

    可用畫圖來表示其堆棧的存取:其中李四把張三給覆蓋啦。

    修改代碼如下:
    public class Person {
        String  name;
        
    int  age;
        
    public static void main(String[] args) {
            Person p1 
    = new Person();
            Person p2 
    = new Person();
            
            p2 
    = p1;
            p1.name 
    = "張三";
            p1.age  
    = 23;
            p2.name 
    = "李四";
            p2.age 
    = 24;
            
            p1.say();
            p2.say();
            
        }

        
    void say(){
            System.out.println(
    "我的名字是:"+ name +"年齡 :"+age);
        }


    }
    運行結(jié)果也是:我的名字是:李四年齡 :24
                                我的名字是:李四年齡 :24
    其堆棧存取用畫圖表示如下:其中李四把張三給覆蓋啦

    p2先斷開自己原先的堆指向,再指向p1的指向,原先的堆空間被釋放。

    posted @ 2010-10-13 08:33 迷人笑笑 閱讀(993) | 評論 (3)編輯 收藏
             java API 是應用編程接口,是一種規(guī)范,指明編寫應用程序的程序員如何訪問類和行為和狀態(tài)。
             下面就列舉個API 中string類中的10個方法的小應用:
            1.  contains:當且僅當此字符串包含指定的char值序列時,返回true
             
    public class Contains {
            
    public static void main(String[] args) {
            String str 
    ="abcdefg";
            
    boolean i = str.contains("ab");
            System.out.print(i);
            
        }


    }
          如果把上面程序中的 boolean i = str.contains("ab")改為 boolean i = str.contains(null) 即把指定的char值序列改為空值,便會出現(xiàn)NullPointExcption的異常。
         
          2.copyValueOf:通過這個方法把給定數(shù)組中的相應的部分copy到字符串中輸出來
    public static void main(String[] args) {
            
    char[] x = {'a','b','c','d','e'};
            String str 
    = null;
            str 
    = String.copyValueOf(x,2,3);
            System.out.print(str);

        }


    }
            str = String.copyValueOf(x,2,3)其中的x表示原數(shù)組,2表示偏移量,3表示要輸出的長度。
     
           3.Intern:返回字符串對象的規(guī)范化表示形式,遵循以下規(guī)則:對于任意兩個字符串s,t,當且僅當s.equals(t) 為true時,s.intern() == t.intern 才為true
    public static void main(String[] args) {
        String str1 
    = "HelloWorld";
        String str2 
    = "Hello"+"World";
        System.out.println(str1 
    == str2);
        System.out.print(str1.intern( ) 
    == str2.intern());
        }


    }
         4.length:返回此對象的長度
    public class Length {

        
        
    public static void main(String[] args) {
            String str 
    = "abcdefg";
            System.out.print(str.length());
        }


    }
          5.matches告知此字符串是否匹配指定的正則表達式,當且僅當匹配給定的正則表達式時,輸出true
    public class Matches {

        
        
    public static void main(String[] args) {
            String str 
    = "abcdefg";
            
    boolean i =str.matches( "abcdefg");
            System.out.println(i);
        }


    }
          6.replaceFirst:用給定的字符串替換正則表達式匹配的字符串
    public class ReplaceFirst {
            
    public static void main(String[] args) {
            String str 
    = "abcdefgh";
            String str1 
    = str.replaceFirst("ef""k");
            System.out.print(str1);
        }


    }

         7.subString:返回一個新字符串,他是此字符串的一個字子符串,該子字符串從指定字符串索引處的字符開始,直到指定的索引結(jié)束的字符結(jié)束
    public class Substring {

        
        
    public static void main(String[] args) {
            String str 
    = "abcdefgh";
            String str1 
    = str.substring(24);
            System.out.print(str1);
        }

    }

    其中String str = "str.substring(2,4)"中2表示起始索引(包括),4表示結(jié)束索引(不包括)
    如果把其實索引改為負數(shù)或結(jié)束索引大于字符串長度,便會出現(xiàn)indexoutBounsException的異常
           8.toCharArray:將此字符串轉(zhuǎn)化為一個新的字符數(shù)組
    public class ToCharArray {
        
    public static void main(String[] args) {
            String str1 
    = "一定要死幸福";
            
    char[] str2 = str1.toCharArray( );
            
    for(int i=0;i<str2.length;i++){
            System.out.println(str2[i]);
            }

        }

    }
          9.trim:此方法可用于截去開頭和尾部的空白
    public class Trim {
        
    public static void main(String[] args) {
        String str 
    = "  abcd   ";
        String strArr 
    = str.trim();
        System.out.println(strArr);
        }


    }
         10.valueOf:返回char參數(shù)的字符串表示形式
         
    public class ValueOf {
        
    public static void main(String[] args) {
        
    char[] x = {'a','b','c','d'};
        String str1 
    ="null";
        str1 
    = String.valueOf(x);
        System.out.print(str1);

        }


    }

    posted @ 2010-10-09 17:48 迷人笑笑 閱讀(442) | 評論 (0)編輯 收藏
         摘要: 要求:寫一java小程序,要求輸出姓名,班級、年齡、分數(shù)、等級 創(chuàng)建三個類Student,StudentText,Text具體程序為: package com.dr.student; public class Student {     private String name; &nb...  閱讀全文
    posted @ 2010-10-09 08:38 迷人笑笑 閱讀(167) | 評論 (0)編輯 收藏
    <2010年10月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    常用鏈接

    留言簿(13)

    隨筆檔案

    文章檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲午夜福利精品久久| 国产aⅴ无码专区亚洲av| 亚洲韩国精品无码一区二区三区 | 精品亚洲一区二区三区在线观看| 久久久久久a亚洲欧洲AV| 亚洲欧洲无码AV不卡在线| 97在线视频免费公开视频| 成年私人影院免费视频网站| 亚洲国产高清精品线久久| 亚洲视频在线观看地址| 美女视频免费看一区二区| 91av视频免费在线观看| 免费a在线观看播放| 亚洲美女视频网址| 全部在线播放免费毛片| 在线看片v免费观看视频777| 久久久久久A亚洲欧洲AV冫| 亚洲狠狠ady亚洲精品大秀| ssswww日本免费网站片| 久草免费在线观看视频| 亚洲精品无码永久在线观看你懂的| 亚洲中文字幕一二三四区| 久久国产精品免费专区| 亚洲VA综合VA国产产VA中| 亚洲AV无码久久久久网站蜜桃 | 亚洲精品伊人久久久久| 中文字幕乱码免费看电影| 国产亚洲精品免费| 亚洲人成网站在线观看播放动漫| 伊人免费在线观看| 免费永久看黄在线观看app| 亚洲制服丝袜精品久久| 久久久久久影院久久久久免费精品国产小说 | 亚洲精品无播放器在线播放| 亚在线观看免费视频入口| 亚洲国产成人影院播放| 亚洲AV无码一区二区三区性色| 国产精品久久久久久久久免费| 久久青青成人亚洲精品| 一级做a爱片特黄在线观看免费看| 成年女人免费视频播放体验区 |