08字節(jié)輸出流緩沖流BufferedOutputStream
* A: 字節(jié)輸出流緩沖流BufferedOutputStream
* a: BufferedOutputStream
* 字節(jié)輸出流的緩沖流
* java.io.BufferedOuputStream 作用: 提高原有輸出流的寫入效率
* BufferedOuputStream 繼承 OutputStream
* 方法,寫入 write 字節(jié),字節(jié)數(shù)組
* 構(gòu)造方法:
* BufferedOuputStream(OuputStream out)
* 可以傳遞任意的字節(jié)輸出流, 傳遞的是哪個字節(jié)流,就對哪個字節(jié)流提高效率
* b: 案例代碼
public class BufferedOutputStreamDemo {
public static void main(String[] args)throws IOException {
//創(chuàng)建字節(jié)輸出流,綁定文件
//FileOutputStream fos = new FileOutputStream("c:\\buffer.txt");
//創(chuàng)建字節(jié)輸出流緩沖流的對象,構(gòu)造方法中,傳遞字節(jié)輸出流
BufferedOutputStream bos = new
BufferedOutputStream(new FileOutputStream("c:\\buffer.txt"));
bos.write(55);
byte[] bytes = "HelloWorld".getBytes();
bos.write(bytes);
bos.write(bytes, 3, 2);
bos.close();
}
}
09字節(jié)輸入流緩沖流BufferedInputStream
* A: 字節(jié)輸入流緩沖流BufferedInputStream
* a: BufferedInputStream
* 字節(jié)輸入流的緩沖流
* 繼承InputStream,標(biāo)準(zhǔn)的字節(jié)輸入流
* 讀取方法 read() 單個字節(jié),字節(jié)數(shù)組
* 構(gòu)造方法:
* BufferedInputStream(InputStream in)
* 可以傳遞任意的字節(jié)輸入流,傳遞是誰,就提高誰的效率
* 可以傳遞的字節(jié)輸入流 FileInputStream
* b: 案例代碼
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException{
//創(chuàng)建字節(jié)輸入流的緩沖流對象,構(gòu)造方法中包裝字節(jié)輸入流,包裝文件
BufferedInputStream bis = new
BufferedInputStream(new FileInputStream("c:\\buffer.txt"));
byte[] bytes = new byte[10];
int len = 0 ;
while((len = bis.read(bytes))!=-1){
System.out.print(new String(bytes,0,len));
}
bis.close();
}
}
10四種文件復(fù)制方式的效率比較
* A:四種文件復(fù)制方式的效率比較
* a: 四中復(fù)制方式
* 字節(jié)流讀寫單個字節(jié) 125250 毫秒
* 字節(jié)流讀寫字節(jié)數(shù)組 193 毫秒 OK
* 字節(jié)流緩沖區(qū)流讀寫單個字節(jié) 1210 毫秒
* 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組 73 毫秒 OK
* b: 案例代碼
public class Copy {
public static void main(String[] args)throws IOException {
long s = System.currentTimeMillis();
copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
long e = System.currentTimeMillis();
System.out.println(e-s);
}
/*
* 方法,實現(xiàn)文件復(fù)制
* 4. 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組
*/
public static void copy_4(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
/*
* 方法,實現(xiàn)文件復(fù)制
* 3. 字節(jié)流緩沖區(qū)流讀寫單個字節(jié)
*/
public static void copy_3(File src,File desc)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
}
/*
* 方法,實現(xiàn)文件復(fù)制
* 2. 字節(jié)流讀寫字節(jié)數(shù)組
*/
public static void copy_2(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
/*
* 方法,實現(xiàn)文件復(fù)制
* 1. 字節(jié)流讀寫單個字節(jié)
*/
public static void copy_1(File src,File desc)throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
}
}
11字符輸出流緩沖流BufferedWriter
* A: 字符輸出流緩沖流BufferedWriter
* a: BufferedWriter
* 字符輸出流緩沖區(qū)流
* java.io.BufferedWriter 繼承 Writer
* 寫入方法 write () 單個字符,字符數(shù)組,字符串
* 構(gòu)造方法:
* BufferedWriter(Writer w)傳遞任意字符輸出流
* 傳遞誰,就高效誰
* 能傳遞的字符輸出流 FileWriter, OutputStreamWriter
* b: 案例代碼
public class BufferedWrierDemo {
public static void main(String[] args) throws IOException{
//創(chuàng)建字符輸出流,封裝文件
FileWriter fw = new FileWriter("c:\\buffer.txt");
BufferedWriter bfw = new BufferedWriter(fw);
bfw.write(100);
bfw.flush();
bfw.write("你好".toCharArray());
bfw.flush();
bfw.write("你好");
bfw.flush();
bfw.write("我好好");
bfw.flush();
bfw.write("大家都好");
bfw.flush();
bfw.close();
}
}
12字符輸出流緩沖流BufferedWriter特有方法newLine
* A: 字符輸出流緩沖流BufferedWriter特有方法newLine
* a: 方法介紹
* void newLine() 寫換行
* newLine()文本中換行, \r\n也是文本換行
* 方法具有平臺無關(guān)性
* Windows \r\n
* Linux \n
* newLine()運行結(jié)果,和操作系統(tǒng)是相互關(guān)系
* JVM: 安裝的是Windows版本,newLine()寫的就是\r\n
* 安裝的是Linux版本,newLine()寫的就是\n
/*
* 將數(shù)據(jù)源 c:\\a.txt
* 復(fù)制到 d:\\a.txt 數(shù)據(jù)目的
* 字節(jié)輸入流,綁定數(shù)據(jù)源
* 字節(jié)輸出流,綁定數(shù)據(jù)目的
*
* 輸入,讀取1個字節(jié)
* 輸出,寫1個字節(jié)
*/
* b: 案例代碼
public class BufferedWrierDemo {
public static void main(String[] args) throws IOException{
//創(chuàng)建字符輸出流,封裝文件
FileWriter fw = new FileWriter("c:\\buffer.txt");
BufferedWriter bfw = new BufferedWriter(fw);
bfw.write(100);
bfw.flush();
bfw.write("你好".toCharArray());
bfw.flush();
bfw.write("你好");
bfw.newLine();
bfw.flush();
bfw.write("我好好");
bfw.newLine();
bfw.flush();
bfw.write("大家都好");
bfw.flush();
bfw.close();
}
}
13字符輸入流緩沖流BufferedReader
* A: 字符輸入流緩沖流BufferedReader
* a: 概述
* 從字符輸入流中讀取文本希停,緩沖各個字符陵且,從而實現(xiàn)字符撩满、數(shù)組和行的高效讀取
* public String readLine() 讀取一個文本行奸焙,包含該行內(nèi)容的字符串,不包含任何行終止符先改,如果已到達(dá)流末尾,則返回 null
14字符輸入流緩沖流BufferedReader讀取文本行
* A: 字符輸入流緩沖流BufferedReader讀取文本行
* a: BufferedReader
* 字符輸入流緩沖流
* java.io.BufferedReader 繼承 Reader
* 讀取功能 read() 單個字符,字符數(shù)組
* 構(gòu)造方法:
* BufferedReader(Reader r)
* 可以任意的字符輸入流
FileReader InputStreamReader
* BufferedReader自己的功能
* String readLine() 讀取文本行 \r\n
* 方法讀取到流末尾,返回null
* b: 小特點
* 獲取內(nèi)容的方法一般都有返回值
* int 沒有返回的都是負(fù)數(shù)
* 引用類型 找不到返回null
* boolean 找不到返回false
* c: 案例代碼
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
int lineNumber = 0;
//創(chuàng)建字符輸入流緩沖流對象,構(gòu)造方法傳遞字符輸入流,包裝數(shù)據(jù)源文件
BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));
//調(diào)用緩沖流的方法 readLine()讀取文本行
//循環(huán)讀取文本行, 結(jié)束條件 readLine()返回null
String line = null;
while((line = bfr.readLine())!=null){
lineNumber++;
System.out.println(lineNumber+" "+line);
}
bfr.close();
}
}
/*
* String line = bfr.readLine();
System.out.println(line);
line = bfr.readLine();
System.out.println(line);
line = bfr.readLine();
System.out.println(line);
line = bfr.readLine();
System.out.println(line);
line = bfr.readLine();
System.out.println(line);
*/
15字符流緩沖區(qū)流復(fù)制文本文件
* A: 字符流緩沖區(qū)流復(fù)制文本文件
* a: 案例代碼
/*
* 使用緩沖區(qū)流對象,復(fù)制文本文件
* 數(shù)據(jù)源 BufferedReader+FileReader 讀取
* 數(shù)據(jù)目的 BufferedWriter+FileWriter 寫入
* 讀取文本行, 讀一行,寫一行,寫換行
*/
public class Copy_1 {
public static void main(String[] args) throws IOException{
BufferedReader bfr = new BufferedReader(new FileReader("c:\\w.log"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\\w.log"));
//讀取文本行, 讀一行,寫一行,寫換行
String line = null;
while((line = bfr.readLine())!=null){
bfw.write(line);
bfw.newLine();
bfw.flush();
}
bfw.close();
bfr.close();
}
}
16IO流對象的操作規(guī)律
* A: IO流對象的操作規(guī)律
* a: 明確一:要操作的數(shù)據(jù)是數(shù)據(jù)源還是數(shù)據(jù)目的。
* 源:InputStream Reader
* 目的:OutputStream Writer
* 先根據(jù)需求明確要讀浩峡,還是要寫。
* b: 明確二:要操作的數(shù)據(jù)是字節(jié)還是文本呢错敢?
* 源:
* 字節(jié):InputStream
* 文本:Reader
* 目的:
* 字節(jié):OutputStream
* 文本:Writer
* c: 明確三:明確數(shù)據(jù)所在的具體設(shè)備翰灾。
* 源設(shè)備:
* 硬盤:文件 File開頭。
* 內(nèi)存:數(shù)組稚茅,字符串纸淮。
* 鍵盤:System.in;
* 網(wǎng)絡(luò):Socket
* 目的設(shè)備:
* 硬盤:文件 File開頭。
* 內(nèi)存:數(shù)組亚享,字符串咽块。
* 屏幕:System.out
* 網(wǎng)絡(luò):Socket
* 完全可以明確具體要使用哪個流對象。
* d: 明確四:是否需要額外功能呢虹蒋?
* 額外功能:
* 轉(zhuǎn)換嗎糜芳?轉(zhuǎn)換流。InputStreamReader OutputStreamWriter
* 高效嗎魄衅?緩沖區(qū)對象峭竣。BufferedXXX
* 已經(jīng)明確到了具體的體系上。
17總結(jié)
* 把今天的知識點總結(jié)一遍晃虫。