缓冲流:
缓冲流要“套接”在相应的节点流至上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
四种缓冲流,常用构造方法为:
代码
BufferedReader(Reader in)
BufferedReader(Reader in, int sz) // sz为自定义缓存区的大小
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in, int sz)
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStram out, int sz)
BufferedReader(Reader in, int sz) // sz为自定义缓存区的大小
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in, int sz)
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStram out, int sz)
例子:
import
java.io.
*
;
public class TestBufferStream1{
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream( " c:/TestBufferStream1.java " );
BufferedInputStream bis = new BufferedInputStream(fis);
int c = 0 ;
System.out.println(bis.read());
System.out.println(bis.read());
bis.mark( 100 );
for ( int i = 0 ;i <= 10 && (c = bis.read()) != - 1 ;i ++ ){
System.out.print(( char )c + " " );
}
System.out.println();
bis.reset();
for ( int i = 0 ;i <= 10 && (c = bis.read()) != - 1 ;i ++ ){
System.out.print(( char )c + " " );
}
bis.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
public class TestBufferStream1{
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream( " c:/TestBufferStream1.java " );
BufferedInputStream bis = new BufferedInputStream(fis);
int c = 0 ;
System.out.println(bis.read());
System.out.println(bis.read());
bis.mark( 100 );
for ( int i = 0 ;i <= 10 && (c = bis.read()) != - 1 ;i ++ ){
System.out.print(( char )c + " " );
}
System.out.println();
bis.reset();
for ( int i = 0 ;i <= 10 && (c = bis.read()) != - 1 ;i ++ ){
System.out.print(( char )c + " " );
}
bis.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
BufferReader类中有一个好用的方法readLine(),该方法返回String。