Java IO:常见Java IO流介绍(ByteArrayInputStream,ObjectInputStream,BufferedInputStream)

在接触 Java IO 流的时候,个人觉得需要首先理解 装饰器模式,否则,当看到 如下的代码 会比较纠结:

InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));

然后,由于 Java 的 IO 流比较多,不同的流可以应对不同情景,下面举例介绍几种常见的流:

FileInputStream
ByteArrayInputStream
ObjectInputStream
BufferedInputStream
DataInputStream

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

你可能感兴趣的:(java-IO)