//缓冲字节输入流:publicstaticvoid testBufferedInputStream ()throwsException{FileInputStream in =newFileInputStream("D:\\myfile\\java\\J2SE\\test\\test1.txt");BufferedInputStream bi =newBufferedInputStream(in);int len =0;byte[] b =newbyte[10];while((len = bi.read(b))!=-1){System.out.println(newString(b,0,len));}
bi.close();
in.close();}//缓冲字节输出流:publicstaticvoidtestBufferedOutputStream()throwsException{FileOutputStream out =newFileOutputStream("D:\\myfile\\java\\J2SE\\test\\test2.txt");BufferedOutputStream bo =newBufferedOutputStream(out);String s ="12345565";
bo.write(s.getBytes());
bo.flush();
bo.close();
out.close();}//复制文件:publicstaticvoidcopyFile()throwsException{BufferedInputStream bi =newBufferedInputStream(newFileInputStream("D:\\myfile\\java\\J2SE\\test\\test1.txt"));BufferedOutputStream bo =newBufferedOutputStream(newFileOutputStream("D:\\myfile\\java\\J2SE\\test\\test3.txt"));int len =0;byte[] b =newbyte[1024];while((len = bi.read(b))!=-1){
bo.write(b,0,len);
bo.flush();}
bo.close();
bi.close();}//缓冲字符流: 将byte[] 换做char[],BufferedReader,BufferedeWriter.
转换流
应用场景:当字节流都为字符时,转换为字符流更高效。
/**
*转换输入流
* @throws Exception
*/publicstaticvoidtestInputStreamReader()throwsException{FileInputStream in =newFileInputStream("D:\\myfile\\java\\J2SE\\test\\test.txt");InputStreamReader ir =newInputStreamReader(in,"utf-8");char[] c =newchar[100];int len =0;while((len = ir.read(c))!=-1){System.out.println(newString(c,0,len));}
ir.close();
in.close();}/**
* 转换输出流
* @throws Exception
*/publicstaticvoidtestOutputStreamWriter()throwsException{FileOutputStream out =newFileOutputStream("D:\\myfile\\java\\J2SE\\test\\test1.txt");OutputStreamWriter ow =newOutputStreamWriter(out,"utf-8");String s ="转换输出流";
ow.write(s);
ow.flush();
ow.close();
out.close();}