package com.播客;
import java.io.*;
public class FileIOStreamDome {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//method1(); //耗时:55596毫秒 字节流一次读取一个字节
//method2(); //耗时:97毫秒 字节流一次读取一个字节数组
//method3(); //耗时:552毫秒 字节缓冲流一次只读写一个字节
//method4(); //耗时:33毫秒 字节缓冲流一次只读写一个字节数组
long end = System.currentTimeMillis();
System.out.println(“耗时:” + (end - start) + “毫秒”);
}
//字节缓冲流一次只读写一个字节数组
private static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路径可绝对也可相对"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路径可绝对也可相对"));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
//释放资源
bis.close();
bos.close();
}
//字节缓冲流一次只读写一个字节
private static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路径可绝对也可相对"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路径可绝对也可相对"));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
//释放资源
bis.close();
bos.close();
}
//字节流一次读取一个字节数组
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("路径可绝对也可相对");//目标源地址
FileOutputStream fos = new FileOutputStream("路径可绝对也可相对");//目的地地址
byte[] bys = new byte[1024];//1024及其整数倍
int len;
while ((len = fis.read(bys)) != -1) {//把读取到的数据赋值给by;判断读取到的数据是否是-1
fos.write(bys, 0, len);
}
//释放资源
fis.close();
fos.close();
}
//字节流一次只读写一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("路径可绝对也可相对");//目标源地址
FileOutputStream fos = new FileOutputStream("路径可绝对也可相对");//目的地地址
int by;
while ((by = fis.read()) != -1) {//把读取到的数据赋值给by;判断读取到的数据是否是-1
fos.write(by);
}
//释放资源
fis.close();
fos.close();
}
}