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.*;

public class BufferedStreamDemo
{
    public static void main(String[] args) throws Exception
    {
        // 从命令行参数中指定文件
        File fSource = new File(args[0]);
        File fDest = new File(args[1]);
        
        // 创建带缓冲的流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fSource));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fDest));
        
        // 提示信息
        System.out.println("copy " + fSource.length() + "bytes");
        
        
        byte[] buf = new byte[1];
        while(bis.read(buf) != -1) // read()返回int类型,返回-1表示已到文件结尾
            bos.write(buf); // 写入数据
            
        // 刷新缓冲区
        bos.flush();
        
        // 关闭流
        bos.close();
        bis.close();
        
        // 提示信息
        System.out.println("copy " + fDest.length() + "bytes finished");
    }
}

6、DataInputStream和DataOutputStream

DataInputStream和DataOutputStream类提供了对Java基本数据类型写入的方法,如int,double,boolean。因为Java中基本数据类型的大小是固定的,不会因为不同的机器而改变,因此在写入的时候就不必担心不同平台数据大小不同的问题。

有一个writeUTF()方法值得我们注意。这个方法会将指定的String对象中的字符写入,但在写入的数据之前会首先写入2个字节的长度数据,这个数据指示了带写入的字符的大小。这样的好处是当我们在使用readUTF()读取数据的时候就不必考虑数据大小的问题了,直接读取就行,因为在readUTF()内部会控制好读取数据的长度。

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


原文地址:https://www.cnblogs.com/javawebsoa/p/2990458.html