java 缓冲流_Java缓冲流的使用
package java;
import org.junit.Test;
import java.io.*;
/*** 處理流之一:緩沖流的使用** 1.緩沖流:* BufferedInputStream* BufferedOutputStream* BufferedReader* BufferedWriter** 2.作用:提供流的讀取、寫入的速度* 提高讀寫速度的原因:內(nèi)部提供了一個(gè)緩沖區(qū)** 3. 處理流,就是“套接”在已有的流的基礎(chǔ)上。**/
public class BufferedTest {
/*實(shí)現(xiàn)非文本文件的復(fù)制*/
@Test
public void BufferedStreamTest() throws FileNotFoundException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件 File srcFile = new File("愛情與友情.jpg");
File destFile = new File("愛情與友情3.jpg");
//2.造流 //2.1 造節(jié)點(diǎn)流 FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩沖流 bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.復(fù)制的細(xì)節(jié):讀取、寫入 byte[] buffer = new byte[10];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
// bos.flush();//刷新緩沖區(qū)
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關(guān)閉 //要求:先關(guān)閉外層的流,再關(guān)閉內(nèi)層的流 if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說明:關(guān)閉外層流的同時(shí),內(nèi)層流也會(huì)自動(dòng)的進(jìn)行關(guān)閉。關(guān)于內(nèi)層流的關(guān)閉,我們可以省略.// fos.close();// fis.close(); }
}
//實(shí)現(xiàn)文件復(fù)制的方法 public void copyFileWithBuffered(String srcPath,String destPath){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件 File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2.造流 //2.1 造節(jié)點(diǎn)流 FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩沖流 bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.復(fù)制的細(xì)節(jié):讀取、寫入 byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關(guān)閉 //要求:先關(guān)閉外層的流,再關(guān)閉內(nèi)層的流 if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說明:關(guān)閉外層流的同時(shí),內(nèi)層流也會(huì)自動(dòng)的進(jìn)行關(guān)閉。關(guān)于內(nèi)層流的關(guān)閉,我們可以省略.// fos.close();// fis.close(); }
}
@Test
public void testCopyFileWithBuffered(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Administrator\\Desktop\\01-視頻.avi";
String destPath = "C:\\Users\\Administrator\\Desktop\\03-視頻.avi";
copyFileWithBuffered(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("復(fù)制操作花費(fèi)的時(shí)間為:" + (end - start));//618 - 176 }
/*使用BufferedReader和BufferedWriter實(shí)現(xiàn)文本文件的復(fù)制*/
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//創(chuàng)建文件和相應(yīng)的流 br = new BufferedReader(new FileReader(new File("dbcp.txt")));
bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
//讀寫操作 //方式一:使用char[]數(shù)組// char[] cbuf = new char[1024];// int len;// while((len = br.read(cbuf)) != -1){// bw.write(cbuf,0,len);// // bw.flush();// }
//方式二:使用String String data;
while((data = br.readLine()) != null){
//方法一:// bw.write(data + "\n");//data中不包含換行符 //方法二: bw.write(data);//data中不包含換行符 bw.newLine();//提供換行的操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉資源 if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java 缓冲流_Java缓冲流的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python回车键绑定按钮_python
- 下一篇: python离线安装依赖包_python