目录
四、处理流:套接在已有的流的基础上(BufferedReader&BufferedWriter)
⚪使用BufferedReader&BufferedWriter实现文本文件的复制
BufferedInputStream
BufferedOutPutStream
BufferedReader
BufferedWriter
一、作用
提高流的读写效率
⚪ 提高读写速度的原因
内部提供了一个缓冲区
二、缓冲流的使用
@Test
public void test1(){
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile = new File("1515050201.jpg");
File destFile = new File("1515050201(3).jpg");
//2.造流
//2.1造两个节点流
fis = new FileInputStream((srcFile));
fos = new FileOutputStream((destFile));
//2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.复制的细节:读取 & 写入
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源关闭
//要求:先关闭外层的流,再关闭内层的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略
// fos.close();
// fis.close();
}
}
三、缓冲流与节点流读写速度对比
1.实现文件复制的方法
public void copyFile(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//复制的过程
byte[] buffer = new byte[1024]; //2^10
int len;
while ((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.方法测试
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Lenovo\\Desktop\\软件2005班张兰.mp4";
String destPath = "C:\\Users\\Lenovo\\Desktop\\软件2005班张兰02.mp4";
CopyFileWithBuffered(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作花费的时间为:" + (end-start)); //复制操作花费的时间为:1501
}
四、处理流:套接在已有的流的基础上(BufferedReader&BufferedWriter)
⚪使用BufferedReader&BufferedWriter实现文本文件的复制
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//创建文件和相应的流
br = new BufferedReader(new FileReader(new File("abab")));
bw = new BufferedWriter(new FileWriter((new File("abab1"))));
//读写操作
//方式1:使用char[]数组
// char[] buffer = new char[10];
// int len;
// while((len = br.read(buffer)) != -1){
// bw.write(buffer,0,len);
// bw.flush();
// }
//方式2:使用String
String data;
while((data = br.readLine()) !=null){
//方法1:
// bw.write(data + "\n"); //data中不包含换行符,需要自己添加换行符
//方法2:
bw.write(data + "\n"); //data中不包含换行符
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}