Buffer byte stream, a character stream buffer

Role: to improve the efficiency of reading and writing

com.study02 Package;
// byte stream buffer

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

{class TestCopy public
public static void main (String [] args) throws IOException {
// the data source. 1.

the FileInputStream new new FIS = the FileInputStream ( "aaaa.txt");
. 2 destination file //
a FileOutputStream fos = new new a FileOutputStream ( "BBB .txt ");
// operations. 3.

@ use of a buffer
BufferedInputStream BIS = new new BufferedInputStream (FIS);
BufferedOutputStream The new new BufferedOutputStream The BOS = (fos);

byte [] = by new new byte [1024];
int len = 0;
the while ( (len = bis.read (by)) = -. 1) {!
bos.write (by, 0, len);
bos.flush ();
}
// close space. 4: Close stream to an upper node.

bos.close ();
bis.close ();
}
}

 

Buffered character stream

package com.study02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class testBuffered {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw= null;
try {
br = new BufferedReader(new FileReader("aaa.txt"));
bw = new BufferedWriter(new FileWriter("bbb.txt"));
String line = null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();//换行
bw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (bw!=null) {
bw.close();
}

if (br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

 

Where plain text with a character stream, otherwise the byte stream using

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/LuJunlong/p/11830512.html