国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

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 -------------------------");  } }}

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱州市| 城口县| 吴川市| 开封县| 射洪县| 黄大仙区| 高陵县| 崇仁县| 米泉市| 松原市| 安仁县| 平原县| 南漳县| 岳阳县| 乌兰县| 日土县| 同德县| 伊吾县| 醴陵市| 南通市| 万荣县| 思南县| 浙江省| 蒙山县| 惠安县| 安岳县| 浮山县| 邵东县| 呈贡县| 澎湖县| 稷山县| 报价| 凌海市| 青浦区| 卢氏县| 托里县| 宝鸡市| 边坝县| 前郭尔| 潼关县| 德庆县|