麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

java溫故而知新(6)深入理解IOStream

2019-11-14 15:32:01
字體:
來源:轉載
供稿:網友

一、什么是IO Stream

Stream 是在編程語言中對輸入輸出的總稱 (一種比喻的稱謂。Stream 為流水,輸入輸出實際上就是指數據的流動,數據由一個地方運動的另一個地方,就像流水一樣,程序員將輸入輸出比作流水,再恰當不過了)。
 流按照其所載內容分類,大致可分為字節流和字符流兩大類。
1、字節流 Byte Stream
在計算機中,byte是相當于機器語言中的單詞,他在java中統一由InputStreamOutputStream作處理。
 
2、字符流(Character Stream
而在編碼體系中,一般采用Char2 bytes他在Java中統一由ReaderWriter作處理。
 
InputStream, OutputStream, ReaderWriter, 作為在java.io.*包的頂級父類,定義了IO PRocess中最抽象的處理和規范。對于實際的應用,他們并不適用。于是根據各種實際的需要,由他們派生出來形式各樣各具特色的子類。
 

二、IO Stream分類

 

1 Node Stream :基本流,可以從名稱中看出他是從哪個地方輸入輸出的。
1.1 用于文件輸入輸出流: FileInputStream, FileOutputStream
1.2 用于內存數組的輸入輸出流:ByteArrayInputStream, ByteArrayOutputStream
1.3 用于字符串的輸入輸出流:StringArrayInputStream, StringArrayOutputStream
1.4 用于管道的輸入輸出流:PipedInputStream, PipeOutStream (用于線程間的交互)
….

 

2 Processing Stream: 處理流,是對Node Stream的加強和補充,可以看作是高級流。 要構造一個高級流通常要以一個基礎流為基礎(如通過構造函數的參數傳入)
2.1 用于提高輸入輸出效率的緩沖流:BufferedInputStream, BufferedOutputStream
2.2 用于數據轉化的數據流: DataInputStream (用于讀取JavaPrimitive Data Type) , DataOutputStream
2.3 8位轉化為16位的流: InputStreamReader, OutputWriter (用于溝通byte Char )
2.4 打印流: PintStream
….

三、IO 編程的一般流程

1. 創建基本流
2. 升級基本流到高級流
3. 使用在高級流中的方法作讀寫操作
4. 關閉流并釋放資源
-------------------------------------------------------------------------------1. Creat node stream;2. Upgrade node stream to processing stream if necessary3. Use the methods in the stream object to read or write4. Close the stream and release the resource------------------------------------------------------------------------------1. Create InputStream/Reader2. Upgrade to Buffered3. Use readLine()   While((str=in.readln())!=null)4. close()------------------------------------------------------------------------------1. Create OutputStream/Writer2. Upgrade to PrintWriter3. Use println()4. close()

 

四、經典IO實例

import java.io.*;/*1. Creat node stream;2. Upgrade node stream to processing stream if necessary3. Use the methods in the stream object to read or write4. Close the stream and release the resource--------------------------------------------------------1. Create InputStream/Reader2. Upgrade to Buffered3. Use readLine()   While((str=in.readln())!=null)4. close()--------------------------------------------------------1. Create OutputStream/Writer2. Upgrade to PrintWriter3. Use println()4. close()*/public class IOProcessSample{ public static void main(String[] args) {  //Create a file based on the first command-line argument to the program  File file= new File(args[0]);  //Create buffered reader from the standard input  BufferedReader in=new BufferedReader(new InputStreamReader(System.in));      System.out.println("Press ctr-d or ctr-z to end");  String str;  try{   //Create a print write to write on a file   //PrintWriter is required to handled the IO exception   PrintWriter out= new PrintWriter(file);   //Read from the standard input and write to the file   while((str=in.readLine())!=null){    out.println(str);   }   //close the stream and release the resource   in.close();   out.close();  }  catch(FileNotFoundException e){   System.err.println("File not found in part 1 : "+file);  }  catch (IOException e){   e.printStackTrace();  }  finally{   System.out.println("-----------Part1 is ended-----------------------");  }  //////////////////////////////////////////////////////////////////////////////  try{   //Create a buffer reader from a file   in=new BufferedReader(new FileReader(file));   //Read the file and print the content on the screen.   while((str=in.readLine())!=null){    System.out.println(str);   }   //close the stream and release the resource   in.close();  }  catch (FileNotFoundException e){   System.err.println("File not found in part 2: "+file);  }  catch (IOException e){   e.printStackTrace();  }  finally{   System.out.println("----------------------The End -------------------------");  } }}

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩黄站 | 免费一级片观看 | 一区二区三区欧美精品 | 亚洲一区二区三区视频免费 | 一本在线高清码电影 | 欧美精品免费一区二区三区 | 青青草免费观看完整版高清 | 色人阁在线视频 | 亚洲午夜国产 | 2017亚洲男人天堂 | 国产一区二区三区手机在线 | 爱逼爱操综合网 | 96视频在线免费观看 | 中文字幕电影免费播放 | 精品久久久久久久久久中文字幕 | 国产午夜亚洲精品理论片大丰影院 | 精品一区二区在线观看 | chengrenzaixian| 欧美.com| 在线无码 | 毛片一级免费看 | 亚州精品在线视频 | 久草在线看片 | 青草久久av | 日本在线视频一区二区三区 | 黄污在线观看 | 黄色毛片免费视频 | 美女黄网站免费观看 | 中文字幕亚洲欧美 | 国产精品免费一区二区三区四区 | 久久99精品久久久久久国产越南 | 午夜a狂野欧美一区二区 | 黄色av电影在线 | 久久99国产精品久久99果冻传媒 | 国产羞羞视频在线观看 | 久久宗合色 | 欧美一级高清片在线 | 成人免费观看毛片 | 黄色大片网站在线观看 | 中文字幕亚洲视频 | hd性videos意大利复古 |