Xiaobo Sun

          Eclipse-Unix http://umlfact.berlios.de/~s_xsun/

          Java Stream

          stream應(yīng)該是水龍頭里的水資源,
          InputStream:是一個出水龍頭(把水封裝在里頭)的一個實物對象,該對象的read方法呢,就想成這
          個"出水龍頭"這一機制對象的開關(guān)鈕,你read或openStream(其他對象包容InputStream對象的對象方法)
          一下呢,就等于打開了出水龍頭的按鈕,水就出來了,里頭封裝的水是什么性質(zhì)的呢,
          你就用相應(yīng)的容器來裝,如string或byte[].....
          OutputStream:你就在InputStream基礎(chǔ)上反著想就ok了
          =====================================================================================
          當(dāng)然,我們可以在Inputstream和OutputStream數(shù)據(jù)源的基礎(chǔ)上,從實際需要觸發(fā),
                來重新封裝出不同性能機制的輸入、輸出流了,java.io包中提供了很豐富的輸入、輸出流對象,如:
               基于字節(jié)流的stream:
                DataOutputStream----DataInputStream:
                   FileOutputStream-----FileInputStream:
          File->inputstream->...
          ...->outputstream->File

                  .............等,可以用InputStream和OutputStream從JDK文檔查閱
               基于字符流的stream(典型的以write 和reader來標(biāo)識的):
                FileWriter---FileReader:
                   StringWriter---StringReader:
                    .........等,你自己可以用Writer和Reader從JDK文檔里頭查看說明
          ======================================================================================

          InputStreamReader r = new InputStreamReader(System. in ); // InputStream from console
          BufferedReader in = new BufferedReader ( r );
          String line ;
          while (( line = in . readLine ()) != null) {
            System. out . println ( "> "+line );
          }

          FileReader r = new FileReader ( args [0]); // InputStream from file
          BufferedReader in = new BufferedReader ( r );
          String line ;
          while (( line = in . readLine ()) != null) {
            System. out . println ( "> "+line );
          }

          FileWriter w = new FileWriter ( args [0]); //OutputStream to file
          BufferedWriter bw = new BufferedWriter (w);
          PrintWriter out = new PrintWriter (bw);
          out . println ( "dies" );
          out . println ( " ... ist ein Log!" );
          out . println ( "Ciao!" );
          out . close (); // schliessen nicht vergessen !
          ===================================================================
          FileInputStream是InputStream的子類,由名稱上就可以知道, FileInputStream主要就是從指定的File中讀取資料至目的地。

          FileOutputStream是OutputStream的子類,顧名思義,F(xiàn)ileOutputStream主要就是從來源地寫入資料至指定的File中。

          標(biāo)準(zhǔn)輸入輸出串流物件在程式一開始就會開啟,但只有當(dāng)您建立一個FileInputStream或FileOutputStream的實例時,實際的串流才會開啟,而不使用串流時,也必須自行關(guān)閉串流,以釋放與串流相依的系統(tǒng)資源。

          下面這個程式可以復(fù)制檔案,程式先從來源檔案讀取資料至一個位元緩沖區(qū)中,然后再將位元陣列的資料寫入目的檔案:

          • FileStreamDemo.java
          package onlyfun.caterpillar;

          import java.io.*;

          public class FileStreamDemo {
          public static void main(String[] args) {
          try {
          byte[] buffer = new byte[1024];

          FileInputStream fileInputStream =
          new FileInputStream(new File(args[0]));
          FileOutputStream fileOutputStream =
          new FileOutputStream(new File(args[1]));

          System.out.println("復(fù)制檔案:" +
          fileInputStream.available() + "位元組");
          while(true) { // 從來源檔案讀取資料至緩沖區(qū)
          if(fileInputStream.available() < 1024) {
          int remain;
          while((remain = fileInputStream.read())
          != -1) {
          fileOutputStream.write(remain);
          }
          break;
          }
          else {
          fileInputStream.read(buffer);
          // 將陣列資料寫入目的檔案
          fileOutputStream.write(buffer);
          }
          }

          // 關(guān)閉串流
          fileInputStream.close();
          fileOutputStream.close();

          System.out.println("復(fù)制完成");
          }
          catch(ArrayIndexOutOfBoundsException e) {
          System.out.println(
          "using: java FileStreamDemo src des");
          e.printStackTrace();
          }
          catch(IOException e) {
          e.printStackTrace();
          }
          }
          }

          這個程式示范了兩個 read() 方法,一個可以讀入指定長度的資料至陣列,一個一次可以讀入一個位元組,每次讀取之后,讀取的指標(biāo)都會往前進,您使用available()方法獲得還有多少位元組可以讀取;除了使用File來建立FileInputStream、FileOutputStream的實例之外,您也可以直接使用字串指定路徑來建立。

          不使用串流時,記得使用close()方法自行關(guān)閉串流,以釋放與串流相依的系統(tǒng)資源。

          posted on 2008-05-19 15:21 Xiaobo Sun 閱讀(961) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: Java Stream 2008-12-16 09:52 Xiaobo Sun

          public static void main(String[] args) throws IOException {
          BufferedReader bufferedReader = new BufferedReader(
          new InputStreamReader(System.in));
          String s;
          while (bufferedReader.read() != -1) {
          System.out.println("input something:");
          s = bufferedReader.readLine();
          System.out.println("input is: " + s);
          }
          }
          ======================================
          java io 有兩種類型的讀寫方法。

          一種是以字節(jié)為單位讀寫的,InputStream、OutputStream(interface) 下面有很多類像FileInputStream、PipedInputStream 等皆繼承了上述接口。

          另外一種就是你上面提到的以unicode為單位進行讀寫的方法。 表現(xiàn)為 Reader、Writer、接口,像BufferedReader 就繼承了Reader接口。

          BufferedReader ****===> 在讀取的時候,會先開辟一個緩沖區(qū),把這些數(shù)據(jù)讀到緩沖區(qū),等到滿了以后,就直接拿出來。

          至于InputStreamReader,****===> An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and translates them into characters according to a specified character encoding.通過這個類可以把這兩種方式讀取出來的信息相互切換。

          system.in ****===> 這是控制臺輸入信息時讀入的管道。她繼承PrintStream ,而PrintStream 類 繼承了FilterOutputStream 類。這個類實現(xiàn)了OutputStream 接口。   回復(fù)  更多評論   

          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 嘉兴市| 海安县| 凯里市| 砚山县| 兰州市| 古田县| 巨鹿县| 旅游| 苗栗县| 龙川县| 拜泉县| 垫江县| 丘北县| 榆林市| 于田县| 金阳县| 巩义市| 古田县| 永吉县| 新巴尔虎左旗| 芦山县| 潼南县| 牡丹江市| 锡林郭勒盟| 博客| 黔东| 北川| 南昌市| 中卫市| 双辽市| 海丰县| 阿图什市| 来凤县| 侯马市| 开江县| 陵水| 丰镇市| 沙洋县| 平江县| 始兴县| 永福县|