以下是一个将整个文件缓冲到OutputStream流中的示例代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Main {
    public static void main(String[] args) {
        String sourceFile = "path/to/source/file.txt";
        String destinationFile = "path/to/destination/file.txt";
        try {
            // 创建输入流和输出流
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
            // 创建缓冲区
            byte[] buffer = new byte[1024];
            int bytesRead;
            // 从输入流读取数据并写入输出流
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            // 关闭流
            fileInputStream.close();
            fileOutputStream.close();
            System.out.println("文件缓冲成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
在上述代码中,我们使用了FileInputStream来读取源文件,并使用FileOutputStream将读取的数据写入目标文件。通过创建一个大小为1024的缓冲区,我们可以一次读取和写入多个字节,以提高效率。最后,我们通过关闭输入流和输出流来释放资源。
请确保将sourceFile和destinationFile替换为实际的文件路径。