IO 文件读取 加入缓冲流
public class BufferedCopy {
public static void main(String[] args) {
copy("F:/java.cx/IO02/src/abc.txt","F:/java.cx/IO02/data.txt");
}
public static void copy(String scrPath, String destPath) {
// 1.创建源
File str = new File(scrPath);// 源头
File data = new File(destPath);// 目的地
// 2.选择流
OutputStream os = null;
InputStream is = null;
// 3.操作
try {
is = new BufferedInputStream (new FileInputStream(str));//缓冲(写速度更快)
os = new BufferedOutputStream(new FileOutputStream(data, true));// 缓冲(读速度更快)
byte[] flush = new byte[1024];// 缓冲容器
int len = -1;// 接收长度
while ((len = is.read(flush)) != -1) {// 3.读取流
os.write(flush, 0, len);// 分段写入
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源 1.先打开的后释放 2.释放由内到外
try {
if (null != os) {//释放资源
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (null != is) {//释放资源
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}