Java IO:基於字節的IO操作

包括:

  1. FileInputStream
  2. ByteArrayInputStream
  3. ObjectInputStream
  4. BufferedInputStream
  5. DataInputStream

上面 5 個爲常用的基於字節操作的 IO 操作類,其中前三個 相當於 裝飾器模式的 ConcreteComponent,後兩個是具體的裝飾者類。傳送門:裝飾器模式


一. FileInputStream
    用於讀取文件內容。常用的有兩種構造函數,例如:
public FileInputStream(String name) throws FileNotFoundException {
    this(name != null ? new File(name) : null);
}

public FileInputStream(File file) throws FileNotFoundException {
    ...
}
    其中 name 和 file 都是指向 需要讀取的文件。Demo:
public class Test{
    public static void main(String[] args) {
        InputStream inputStream = null;
	try {
	    inputStream = new FileInputStream("/Users/yunxin/Desktop/a");
	    byte data[] = new byte[inputStream.available()];
	    inputStream.read(data);
	    System.out.println(new String(data));
			
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if(inputStream != null){
	        try {
		    inputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	} 
    }
}
    需要注意的是 try-catch-finally

二. ByteArrayInputStream
    ByteArrayInputStream,主要是應對流的來源和目的地不一定是文件這種情況,比如說可能是內存,可能是數組。例如:
public class Test{
    public static void main(String[] args) {
	byte data[] = "abc".getBytes();
	InputStream inputStream = new ByteArrayInputStream(data);
		
	byte data0[] = new byte[data.length]; 
	try {
	    inputStream.read(data0);
	    System.out.println(new String(data0));
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if (inputStream != null) {
		try {
		    inputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
    }
}

三. ObjectInputStream
    ObjectInputStream 可以用於讀取對象,但是讀取的對象必須實現 Serializable 接口
public class Test{
    public static void main(String[] args) {
	ObjectOutputStream objectOutputStream = null;
	try {
	    objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
	    User user = new User();
	    user.setNum(2);
	    objectOutputStream.writeObject(user);
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if(objectOutputStream != null){
	        try {
		    objectOutputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
    }
}
class User implements Serializable{  
    private static final long serialVersionUID = -8987587467273881932L;  
    private int num;  
    public int getNum() {  
        return num;  
    }  
    public void setNum(int num) {  
        this.num = num;  
    }  
    @Override  
    public String toString() {  
        return "User [num=" + num + "]";  
    }     
}  

四. BufferedInputStream

    BufferedInputStream 提供了一個緩衝的功能,可以避免大量的磁盤IO。因爲像FileInputStream 這種,每一次的讀取,都是一次磁盤IO。
public class Test{
    public static void main(String[] args) {
	BufferedInputStream bufferedInputStream = null;
	try {
	    bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/yunxin/Desktop/a"));
	    byte data[] = new byte[bufferedInputStream.available()];
	    bufferedInputStream.read(data);
	    System.out.println(new String(data));
			
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if(bufferedInputStream != null){
	        try {
		    bufferedInputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	} 
    }
}

五. DataInputStream
    該類的主要作用是可以返回一些基本類型或者是String類型,否則的話,只能返回byte類型的數據,利用該類,我們可以更好的操作數據。
public class Test{
    public static void main(String[] args) {
        DataOutputStream dataOutputStream = null;
	try {
	    dataOutputStream = new DataOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
	    dataOutputStream.writeInt(999);
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if(dataOutputStream != null){
	        try {
		    dataOutputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	} 
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章