主要内容
缓冲流
缓冲流,也叫高效流,是对4个基本的 FileXxx 流的增强,所以也是4个流,按照数据类型分类
字节缓冲流: BufferedInputStream , BufferedOutputStream
字符缓冲流: BufferedReader , BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO 次数,从而提高读写的效率。
字节缓冲流
构造方法
public BufferedInputStream(InputStream in) :创建一个 新的缓冲输入流。 public BufferedOutputStream(OutputStream out) : 创建一个新的缓冲输出流。
1.字节缓冲输出流:
public static void main ( String[ ] args) throws IOException {
FileOutputStream fos = new FileOutputStream ( "untitled-jiuyeban10\\cmd\\a.txt" ) ;
BufferedOutputStream bos = new BufferedOutputStream ( fos) ;
bos. write ( "我把数据写进缓冲区" . getBytes ( ) ) ;
bos. flush ( ) ;
bos. close ( ) ;
}
}
2.字节缓冲输入流
public static void main ( String[ ] args) throws IOException {
FileInputStream fis = new FileInputStream ( "untitled-jiuyeban10\\cmd\\a.txt" ) ;
BufferedInputStream bis = new BufferedInputStream ( fis) ;
byte [ ] bytes= new byte [ 1024 ] ;
int a= 0 ;
while ( ( a= bis. read ( bytes) ) != - 1 ) {
System. out. println ( new String ( bytes, a, 0 ) ) ;
}
bis. close ( ) ;
}
}
3.字符缓冲输出流
public static void main ( String[ ] args) throws IOException {
BufferedWriter bw = new BufferedWriter ( new FileWriter ( "untitled-jiuyeban10\\c.txt" ) ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
bw. write ( "传智播客" ) ;
bw. write ( "\r\n" ) ;
bw. newLine ( ) ;
}
bw. flush ( ) ;
bw. close ( ) ;
}
}
4.字符缓冲输入流
public static void main ( String[ ] args) throws IOException {
BufferedReader br = new BufferedReader ( new FileReader ( "untitled-jiuyeban10\\io 10\\c.txt" ) ) ;
String lien;
while ( ( lien= br. readLine ( ) ) != null) {
System. out. println ( lien) ;
}
br. close ( ) ;
}
}
5.文本排序
public static void main ( String[ ] args) throws IOException {
List< String> list = new ArrayList < > ( ) ;
BufferedReader br = new BufferedReader ( new FileReader ( "untitled\\io 10\\操作件.txt" ) ) ;
String str;
while ( ( str= br. readLine ( ) ) != null) {
list. add ( str) ;
}
br. close ( ) ;
BufferedWriter bw = new BufferedWriter ( new FileWriter ( "untitled\\io 10\\操作件.txt" ) ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
String s = list. get ( i) ;
bw. write ( ( i+ 1 ) + s) ;
bw. newLine ( ) ;
bw. flush ( ) ;
}
bw. close ( ) ;
}
}
5.编码案例
public static void main ( String[ ] args) throws IOException {
BufferedReader br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( "C:\\Users\\HKS\\Desktop\\源文件.txt" ) , "GBK" ) ) ;
BufferedWriter bw = new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( "untitled-jiuyeban10\\io 10\\源文件.txt" ) ) ) ;
String str;
while ( ( str= br. readLine ( ) ) != null) {
bw. write ( str) ;
bw. newLine ( ) ;
bw. flush ( ) ;
}
br. close ( ) ;
bw. close ( ) ;
}
}
6.序列化和反系列化
public static void main ( String[ ] args) throws IOException {
List< hero> list = new ArrayList < > ( ) ;
list. add ( new hero ( "张三" , 18 ) ) ;
list. add ( new hero ( "李四" , 38 ) ) ;
list. add ( new hero ( "王五" , 28 ) ) ;
ObjectOutputStream cos = new ObjectOutputStream ( new FileOutputStream ( "开始文件.txt" ) ) ;
cos. writeObject ( list) ;
cos. close ( ) ;
}
}
public static void main ( String[ ] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( "开始文件.txt" ) ) ;
Object o = ois. readObject ( ) ;
ois. close ( ) ;
List< hero> list= ( List< hero> ) o;
for ( hero hero : list) {
System. out. println ( hero) ;
}
}
}
7.字节打印流使用方法加键盘录入
public static void main ( String[ ] args) throws FileNotFoundException {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "录入一行字符串" ) ;
PrintStream ps = new PrintStream ( "untitled-jiuyeban10\\io 10\\ps.txt" ) ;
String ss = sc. nextLine ( ) ;
ps. println ( 123132 ) ;
ps. close ( ) ;
}
}