Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令
在上一节中,我们使用FileInputStream类和FileOutputStream类来实现了一个可以自由拷贝文件的功能。为了提高效率,我们人为地定义一个缓冲区byte[] 数组。其实,我们可以使用BufferedInputStream类和BufferedOutputStream类来重写这个功能。
5、BufferedInputStream、BufferedOutputStream
看到Buffererd这个词,我们或许可以猜到,这两个类应该是带有缓冲区的流类。正如我们所想的那样,它们确实有一个buf数据成员,是一个字符数组,默认大小为2048字节。当我们在读取数据时,BufferedInputStream会尽量将buf填满;使用read()方法读取数据时,实际上是先从buf中读取数据,而不是直接从数据来源(如硬盘)上读取。只有当buf中的数据不足时,BufferedInputStream才会调用InputStream的read()方法从指定数据源中读取。
BufferedOutputStream的数据成员buf是一个512字节的字节数组,当我们调用write()方法写入数据时,实际上是先向buf中写入,当buf满后才会将数据写入至指定设备(如硬盘)。我们也可以手动地调用flush()函数来刷新缓冲区,强制将数据从内存中写出。
下面用这两个类实现文件复制功能:
package cls;import java.io.*;class Student{ String name; int score; // 构造方法 public Student(String name,int score) { this.name = name; this.score = score; } // 返回名字 public String getName() { return name; } // 返回分数 public int getScore() { return score; }}public class DataStreamDemo{ public static void main(String[] args) throws Exception { // 创建3个Student对象 Student[] sd = new Student[]{new Student("dog",100),new Student("pig",200),new Student("cat",300)}; // 创建输出流对象 DataOutputStream dos = new DataOutputStream(new FileOutputStream(args[0])); //向文件中写入 // 使用增强for循环写入数据 for(Student st : sd) { dos.writeUTF(st.getName()); // 写入String dos.writeInt(st.getScore()); } dos.flush(); // 刷新缓冲区 dos.close(); // 关闭流 // 从文件中读入数据 DataInputStream dis = new DataInputStream(new FileInputStream(args[0])); for(int i = 0 ; i < 3 ; ++i) { System.out.println(dis.readUTF()); // 取入String字符串,不必担心长度的问题 System.out.println(dis.readInt()); } dis.close(); }}