使用缓冲流 BufferedOutputStream 写入文件
FileOutputStream fos = new FileOutputStream("/Users/Desktop/Test/Test.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("HelloWorld".getBytes());
bos.close();
使用缓冲流 BufferedIntputStream 读取文件
FileInputStream fis = new FileInputStream("/Users/Desktop/Test/Test.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
System.out.println(new String(b, 0, len));
}
bis.close();
测试,比较使用缓冲流复制文件和不使用缓冲流复制文件所用时间差距
public class Test{
public static void main(String[] args) throws IOException {
// 测试
new fun1.printTime();
new fun2.printTime();
new fun3.printTime();
new fun4.printTime();
}
}
// 程序的开始部分和结尾部分均相同,创建抽象类
abstract class TestTime {
// 源文件路径
public String src = "/Users/DeskTop/Test/Test.png";
// 目的文件路径
public String dest = "/Users/DeskTop/Test/Tests.png";
// 统计花费时间方法
public void printTime() throws IOException {
// 获取程序开始时间
long start = System.currentTimeMillis();
// 调用抽象方法复制文件
copyFile();
// 获取程序结束时间
long end = System.currentTimeMillis();
}
// 抽象方法,字符流/缓冲流复制文件
public abstract void copyFile() throws IOException;
// 抽象方法,打印两种方法所用时间
public abstract void printSpeedTimes(long start, long end);
}
// 字符流复制文件
abstract class Mycopy1 extends TestTime {
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
int len = 0;
// 调用内部实现的抽象方法
run1(len, fis, fos);
fis.close();
fos.close();
}
// 内部实现的抽象方法
public abstract void run1(int len, FileInputStream fis, FileOutputStream fos) throws IOException);
public abstract void printSpeedTimes(long start, long end);
}
// 字节流单字节内部实现
class fun1 extends Mycopy1 {
public void run1(int len, FileInputStream fis, FileOutputStream fos) throws IOException {
while((len = fis.read()) != -1){
fos.write((char)len);
}
}
// 花费时间
public void printSpeedTimes(long statr, long end){
System.out.println("字节流单字节复制耗费时间:" + (end - start));
}
}
// 字节流数组
class fun2 extends Mycopy1 {
public void run1(int len, FileInputStream fis, FileOutputStream fos) throws IOException {
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
}
public void printSpeedTimes(long start, long end) {
System.out.println("字节流数组复制耗费时间:" + (end - start));
}
}
// 使用缓冲流单字节复制文件
abstract class Mycopy2 extends TestTime {
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
run2(bis, bos, len);
bis.close();
bos.close();
}
public abstract void printSpeedTimes(long start, long end) ;
public abstract void run2(BufferedInputStream bis, BufferedOutputStream bos, int len) throws IOException;
}
class fun3 extends Mycopy2 {
public void run2(BufferedInputStream bis, BufferedOutputStream bos, int len) throws IOException {
while ((len = bis.read()) != -1) {
bos.write(len);
}
}
public void printSpeedTimes(long start, long end) {
System.out.println("缓冲流字节复制耗费时间:" + (end - start));
}
}
class fun4 extends Mycopy2{
@Override
public void printSpeedTimes(long start, long end) {
System.out.println("缓冲流数组复制耗费时间:" + (end - start));
}
public void run2(BufferedInputStream bis, BufferedOutputStream bos, int len) throws IOException {
byte[] b = new byte[1024];
while ((len = bis.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
}
}
缓冲字符流
缓冲字符流
BufferedWriter
构造方法:
参数: Writer(父类)
可传 FileWriter OutputStreamWriter
特有方法
newLine() 此方法无关平台性
mac 为 \n
Windows 为 /r/n
BUfferedReader