进阶day10-字节缓冲流的基本使用以及使用缓冲流复制文件(一次读写一个字节)
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package com.inmind.buffered_stream01;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.*;
|
||||
|
||||
/*
|
||||
4个流本身是没有读写功能,都是基于昨天的字节流和字符流进行操作,所以,今天只要先区分到底是什么流,然后就调用对应流的api
|
||||
@@ -20,9 +18,30 @@ import java.io.FileNotFoundException;
|
||||
int read() :读取一个字节数据
|
||||
int read(byte[] bytes) : 读取一个字节数组
|
||||
|
||||
|
||||
字节缓冲输出流:BufferedOutputStream
|
||||
构造方法:
|
||||
BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
|
||||
常用方法:
|
||||
void write(int b) 将指定的字节写入缓冲的输出流。
|
||||
public void write(byte[] b)
|
||||
void write(byte[] b, int off, int len) 从偏移量 off开始的指定字节数组写入 len字节到缓冲输出流。
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
//创建流对象
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
|
||||
//调用写方法
|
||||
bos.write(97);
|
||||
bos.write("我是谁我在哪".getBytes());
|
||||
bos.write("我在干嘛,哈哈".getBytes(),0,12);
|
||||
|
||||
//释放资源
|
||||
bos.close();
|
||||
}
|
||||
|
||||
|
||||
public static void testBufferedInputStream(String[] args) throws Exception {
|
||||
//IO流的操作
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
|
||||
|
||||
|
||||
45
javaSE-day10/src/com/inmind/buffered_stream01/Demo02.java
Normal file
45
javaSE-day10/src/com/inmind/buffered_stream01/Demo02.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.inmind.buffered_stream01;
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class Demo02 {
|
||||
//使用字节缓冲输入输出流复制一个文件,并计算出时间(一次读写一个字节)
|
||||
public static void main(String[] args) throws IOException {
|
||||
/*FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");*/
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\io_test\\upload\\1.jpg"));
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\io_test\\upload\\3.jpg"));
|
||||
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
int c;//用来保存读取的字节数据
|
||||
while ((c = bis.read()) != -1) {
|
||||
bos.write(c);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
System.out.println("使用缓冲流复制文件花费:"+(end- start));//38
|
||||
|
||||
bis.close();
|
||||
bos.close();
|
||||
}
|
||||
|
||||
|
||||
//使用原始的字节输入输出流复制一个文件,并计算出时间(一次读写一个字节)
|
||||
public static void method1(String[] args) throws IOException {
|
||||
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");
|
||||
long start = System.currentTimeMillis();
|
||||
int c;//用来保存读取的字节数据
|
||||
while ((c = fis.read()) != -1) {
|
||||
fos.write(c);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
System.out.println("使用字节流复制文件花费:"+(end- start));//5880
|
||||
|
||||
fis.close();
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user