日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java 缓冲流_Java缓冲流的使用

發(fā)布時間:2025/3/15 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 缓冲流_Java缓冲流的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

package java;

import org.junit.Test;

import java.io.*;

/*** 處理流之一:緩沖流的使用** 1.緩沖流:* BufferedInputStream* BufferedOutputStream* BufferedReader* BufferedWriter** 2.作用:提供流的讀取、寫入的速度* 提高讀寫速度的原因:內(nèi)部提供了一個緩沖區(qū)** 3. 處理流,就是“套接”在已有的流的基礎(chǔ)上。**/

public class BufferedTest {

/*實現(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é)點流 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)閉外層流的同時,內(nèi)層流也會自動的進(jìn)行關(guān)閉。關(guān)于內(nèi)層流的關(guān)閉,我們可以省略.// fos.close();// fis.close(); }

}

//實現(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é)點流 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)閉外層流的同時,內(nèi)層流也會自動的進(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ù)制操作花費的時間為:" + (end - start));//618 - 176 }

/*使用BufferedReader和BufferedWriter實現(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。