天天看點

java語言基礎(81)——IO流概述、分類及基本使用

java操作流的對象都在io包下。

IO流的用途:用來處理裝置之間的資料傳輸。

IO流的分類:

  按流向分:

      輸入流(讀取資料)

      輸出流(寫資料)

  按資料類型分:

      字元流

          字元輸入流  Reader

          字元輸出流  Writer

      位元組流

          位元組輸入流  InputStream

          位元組輸出流  OutputStream

FileOutputStream(位元組輸出流)的使用:

package Stream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {

  public static void main(String[] args) {
     String s = "hello world";
     FileOutputStream fos = null;
    try {
      // 如果檔案不存在會自動建立
       fos = new FileOutputStream("E:\\fos.txt");
      
      // 追加寫入
      // FileOutputStream fos = new FileOutputStream("E:\\fos.txt",true);
        fos.write(s.getBytes());
        
        // 換行符 windows \r\n  linux \n  Mac \r
        fos.write("\r\n".getBytes());
        fos.write(s.getBytes());
        
        // 為了讓代碼移植性更強,
        //我們用java給的方法智能擷取系統換行符
        fos.write(System.getProperty("line.separator").getBytes());
        fos.write(s.getBytes());
        
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
       // 為了保證close一定可以執行是以放在finally中
      if(fos!=null){
        try {
           // 釋放資源
          fos.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
   
  }

}      

FileInputStream(位元組輸入流)的使用:

package Stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo {
 
  public static void main(String[] args) {
    
     FileInputStream fis = null;
    try {
      fis = new FileInputStream("E:\\fos.txt");
      // 一次讀取一個位元組數組
      byte[] bys = new byte[1024];
      int len = 0;
            while((len = fis.read(bys))!=-1){
              System.out.print(new String(bys,0,len));
            }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{ 
    if(fis!=null){  try {
        fis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }}

  }

}      

位元組緩沖區流(又稱高效位元組流)用法跟FileOutputStream  FileInputStream類似,但效率更高:

BufferedOutputStream  高效位元組輸出流

package Stream;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutputStreamDemo {

  public static void main(String[] args) {
    BufferedOutputStream bos = null;
    try {
      bos = new BufferedOutputStream(new FileOutputStream("E:\\bos.txt"));
        bos.write("hello world hello java".getBytes());
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
    if(bos!=null){  try {
        bos.close();
      } catch (IOException e) {
  
        e.printStackTrace();
      }
    }}
     
  }

}      

BufferedInputStream  高效位元組輸入流

package Stream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BufferedInputStreamDemo {

   
  public static void main(String[] args) {
    BufferedInputStream bis = null;
    try {
      bis = new BufferedInputStream(new FileInputStream("E:\\bos.txt"));
        byte[] bys = new byte[1024];
        int len = 0;
        while((len = bis.read(bys))!=-1){
          System.out.print(new String(bys,0,len));      
        }
    
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      if(bis!=null){
        try {
          bis.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

  }

}      

OutputStreamWriter  字元輸出流

package Stream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
 
  public static void main(String[] args) throws IOException {
       OutputStreamWriter osw = new OutputStreamWriter(new 
           FileOutputStream("E:\\osw.txt"));
       osw.write("我喜歡java");
       osw.close();
  }

}      

InputStreamReader  字元輸入流

package Stream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {

 
  public static void main(String[] args) throws IOException {
     InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\osw.txt"));
         
     // 讀資料 方式一  一次讀取一個字元
//     int ch = 0;
//     while((ch = isr.read())!=-1){
//       System.out.print((char)ch);
//     }
     
     // 讀資料 方式二  一次讀取一個字元數組
     char[] chs = new char[1024];
     int len = 0;
     while((len=isr.read(chs))!=-1){
       System.out.print(new String(chs,0,len));
     }
     
     // 關閉資源
     isr.close();
  }

}      

字元流的便捷操作類:FileWriter  和 FileReader

高效字元流:

 BufferedWriter  字元緩沖輸出流  

 BufferedReader  字元緩沖輸入流

用法與位元組緩沖流類似。