Java中的IO流之輸入流|樂字節

親愛的樂字節的小夥伴們,小樂又來分享Java技術文章了。上一篇寫到了IO流,這篇文章着重 談談輸入流,再下次再說輸出流。java

點擊回顧上一篇:樂字節Java之file、IO流基礎知識和操做步驟數組

 

1、 輸入流

字節流和字符流的操做方式幾乎徹底同樣,只是操做的數據單元不一樣而已 。字節流可jvm

以操做全部文件,字符流僅操做純文本。性能

一、抽象類:InputStream 和 Reader

InputStream和Reader是全部輸入流的基類,它們是兩個抽象類,是全部輸入流的模版,其中定義的方法在全部輸入流中均可以使用。學習

在InputStream裏包含以下三個方法:編碼

在Reader中包含以下三個方法:spa

對比InputStream和Reader 所提供的方法,能夠看出這兩個基類的功能基本類似。返回結果爲-1 時代表到了輸入流的結束點。 InputStream 和 Reade 都是抽象的,不能直接建立它們的實例,可使用它們的子類。code

二、文件節點類: FileInputStream 和 FileReader

FileInputStream 和 FileReader,它們都是節點流,直接和指定文件關聯。 操做方式對象

基本一致。blog

1)、單個字節讀取

以FileInputStream爲例:

public class SingleFileRead { public static void main(String[] args) { // 一、創建聯繫 File對象  File file = new File("f:/IO/test.txt"); // 二、選擇流  InputStream in = null;// 提高做用域  try { in = new FileInputStream(file); // 三、操做 單個字節讀取  long fileLength = file.length(); // 接收實際讀取的字節數  // 計數器  System.out.println(fileLength); long num = 0; // 循環讀取  while (num < fileLength) { char ch = (char) in.read(); System.out.println(ch); num++; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("文件不存在,不能進行下一步操做"); } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("讀取文件失敗"); } finally { try { // 四、釋放資料  if (in != null) { in.close(); } } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("關閉文件輸入流失敗"); } } } } 樂字節原創

 

2)、批量讀取(字節|字符重點)

public class ReadFile { public static void main(String[] args) { //一、字節讀取:創建聯繫 File對象  File file=new File("f:/IO/test.txt"); //二、選擇流  InputStream in=null;//提高做用域  try { in=new FileInputStream(file); //三、操做 不斷讀取 緩衝數組  byte[]car=new byte[1024]; int len=0; //接收實際讀取的大小  //循環讀取  while(-1!=(len=in.read(car))){ //輸出,字節數組轉成字符串  String info=new String(car,0,len); System.out.println(info); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("文件不存在"); } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("讀取文件失敗"); }finally{ try { //四、釋放資料  if(in!=null){ in.close(); } } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); System.out.println("關閉文件輸入流失敗"); } } } } //字符讀取一、建立源  File src=new File("f:/char.txt"); //二、選擇流  Reader reader=new FileReader(src); //三、讀取操做  char[] flush=new char[1024]; int len=0; while(-1!=(len=reader.read(flush))){ //字符數組轉換爲字符串  String str=new String(flush,0,len); System.out.println(str); } //四、釋放資源  reader.close(); 樂字節原創

 

三、緩衝處理類:BufferedInputStream和 BufferedReader(重點)

緩衝提升性能: 字節流直接套上便可;字符緩衝流 +新增方法(不能使用多態)

//一、建立源,創建聯繫 File src =new File("test.txt"); //二、選擇緩衝流  InputStream is =new BufferedInputStream(new FileInputStream(src)); //三、操做 : 多個讀取  byte[] car =new byte[2]; int len =0; while(-1!=(len=is.read(car))){ //獲取數組的內容 字節數組轉字符串 new String(字節數組,0,length)  System.out.println(new String(car,0,len)); } //四、釋放資源  is.close(); //建立源:  File src =new File("test.txt"); //使用字符緩衝流 提升性能讀取文件 +新增方法(不能使用多態)  BufferedReader br =new BufferedReader(new FileReader(src)); //操做 行讀取 String line=null; while(null!=(line=br.readLine())){ System.out.println(line); } //釋放資源 br.close();

 

四、轉換處理流: InputStreamReader

轉換流:將字節流轉爲字符流 處理亂碼(編碼集、解碼集)。

//讀取文件  File src =new File("test.txt"); //轉換流  BufferedReader br =new BufferedReader( new InputStreamReader( new BufferedInputStream( new FileInputStream( src ) ),"utf-8" ) ); //行讀取  String msg =null; while(null!=(msg =br.readLine())){ System.out.println(msg); } br.close();

五、字節數組節點類: ByteArrayInputStream

操做的節點爲字節數組,數組在jvm 內存中,由垃圾回收機制管理,不須要手動關閉

//一、建立源  byte[] src ="io 學習入門".getBytes(); //二、選擇流 InputStream is = new ByteArrayInputStream(src); //三、操做 與文件一致  byte[] flush =new byte[10]; int len =0; while(-1!=(len =is.read(flush))){ System.out.println(new String(flush,0,len)); } //四、釋放  is.close();

六、數據處理流:DataInputStream

能夠處理基本類型+String,保留數據的類型。前提是讀取順序與寫出順序一致,不然讀取數據不正確

/**  * 數據+類型 輸出到文件  * @param destPath  * @throws IOException  */ public static void write(String destPath) throws IOException{ int point=2; long num=100L; String str="數據類型"; //建立源  File dest=new File(destPath); //選擇流  DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); //操做 寫出的順序 爲讀取做準備  dos.writeInt(point); dos.writeLong(num); dos.writeUTF(str); dos.flush(); //釋放資源  dos.close(); }

 

七、對象處理流(反序列化):ObjectInputStream

/**  * 反序列化:  * 一、先寫入再讀取  * 二、讀取對象須要知道具體類型,依次讀取  * 注意:  * 1)不是全部的對象均可以序列化 Serializable  * 2)不是全部的屬性都須要序列化 transient  */ public static void read(String srcPath) throws FileNotFoundException, IOException, ClassNotFoundException{ //建立源  File src=new File(srcPath); //選擇流 OjbectInputStream  ObjectInputStream dis=new ObjectInputStream( new BufferedInputStream( new FileInputStream(src) ) ); //操做 讀取的順序與寫出的順序一致 必須存在才能讀取  Object obj=dis.readObject(); if(obj instanceof Employee){ Employee emp=(Employee)obj; System.out.println(emp.getName()); System.out.println(emp.getSalary()); } obj=dis.readObject(); int[]arr=(int[])obj; System.out.println(Arrays.toString(arr)); //釋放資源  dis.close(); }

Java中的IO流-輸入流就介紹到這裏了,下次再說輸出流。

樂字節原創,更多Java技術乾貨持續更新,歡迎關注。

相關文章
相關標籤/搜索