package com.atguigu.java;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.print.attribute.standard.Destination;
import org.junit.Test;
/*
* 抽象基類 節(jié)點(diǎn)流(文件流) 緩沖流(處理流的一種,可以提高文件處理效率)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream (flus())
* Reader FileReader BufferedReader (readLine())
* Writer FileWriter BufferedWriter (flus())(bw.newLine())
*/
public class testBuffered {
@Test
public void testBufferedReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("07.txt");
File file1 = new File("08.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while((len = br.read(c))!= -1){
// String str = new String(c, 0, len);
// System.out.println(str);
// }
String str;
while((str = br.readLine()) != null){
// System.out.println(str);
bw.write(str);//上面是在一行讀出來的,現(xiàn)在寫也不會(huì)換行,加入新方法
// System.out.println();//不換行
// bw.write(str + "\n");//這種方法換行也可以
bw.newLine();
bw.flush();
}
} catch (Exception 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();
}
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String src = "C:\\Users\\xiaotinh\\Desktop\\03.jpg";
String dest = "C:\\Users\\xiaotinh\\Desktop\\05.jpg";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("文件復(fù)制的時(shí)間:" + (end - start));//15ms
}
//使用緩沖流實(shí)現(xiàn)文件的復(fù)制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供讀入、寫出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.先創(chuàng)建相應(yīng)的節(jié)點(diǎn)流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.將創(chuàng)建的節(jié)點(diǎn)流的對(duì)象作為形參傳遞給緩沖流的構(gòu)造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具體的實(shí)現(xiàn)文件復(fù)制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();//最好后面都加一句這個(gè),因?yàn)榍懊鎺状味紝懗鋈チ耍? //但是到最后一句的時(shí)候,可能沒有寫滿,這時(shí)需要清空一下,防止對(duì)下次有影響
}
} catch (Exception e) {
e.printStackTrace();
} finally{
//5.關(guān)閉相應(yīng)的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用BufferedInputStream和BufferedOutputStream實(shí)現(xiàn)非文本文件的復(fù)制
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供讀入、寫出的文件
File file1 = new File("C:\\Users\\xiaotinh\\Desktop\\03.jpg");
File file2 = new File("C:\\Users\\xiaotinh\\Desktop\\04.jpg");
//2.先創(chuàng)建相應(yīng)的節(jié)點(diǎn)流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.將創(chuàng)建的節(jié)點(diǎn)流的對(duì)象作為形參傳遞給緩沖流的構(gòu)造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具體的實(shí)現(xiàn)文件復(fù)制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
//5.關(guān)閉相應(yīng)的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
IO4:Buffered~--java30(02/25/2016)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 《四個(gè)大爺歷“險(xiǎn)”記》--啾啾通話后帶給我的新心刺激點(diǎn)(2016年10月25日)武立啾啾美拍實(shí)踐落地-三大爺瑞康篇...
- 一切都是用實(shí)力說話。 戰(zhàn)爭(zhēng)時(shí)期如此,沒有現(xiàn)場(chǎng)上的實(shí)力,現(xiàn)場(chǎng)外的一切輿論和談判都是無效的。 在工作場(chǎng)合,沒有干事和交...
- 在一個(gè)環(huán)境中呆久了,就會(huì)漸漸地適應(yīng)它,并且用各種各樣的理由將其合理化。而這個(gè)時(shí)候,能否跳出來,重新審視自己并作出改...