缓冲区
BufferedInputStream 缓冲输入流
BufferedOutputStream 缓冲输出流
用缓冲字节流将其他字节流包装起来 提高效率
缓冲输入流
public static void main(String[] args) {
File f = new File("C:\\Users\\admin\\Desktop\\管老师.txt");//随便找一个较大的文件
BufferedInputStream bi =null;//大大提高了运行效率
FileInputStream in = null;
long start = System.currentTimeMillis();//获取流开始时毫秒值
try {
in = new FileInputStream(f);
bi = new BufferedInputStream(in);//将文件字节流包装成缓冲流
byte b[]= new byte[1024];//缓冲区字节数组与Buffered不同
while (bi.read(b)!=-1) { //使用缓冲流读取数据
}
long end = System.currentTimeMillis();//获取流结束时毫秒值
System.out.println("运行经历的毫秒数:"+(end-start));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bi!=null) {
try {
bi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*/
缓冲输出流
public static void main(String[] args) {
File f = new File("word.txt");//创建一个文件
FileOutputStream out = null;
BufferedOutputStream bo = null;
try {
out = new FileOutputStream(f);
bo = new BufferedOutputStream(out);//将文件输出流包装
String str = "天生我才必有用";
byte b[]=str.getBytes();//将字符串装换成字节数组
bo.write(b);
//使用缓冲字节输出流时,要多进行刷新操作
bo.flush();//刷新 强制的将缓冲区数据写入文件中,即使缓冲区没有写满
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bo!=null) {
try {
bo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
15.5.2缓冲字符流
//BufferedReader 缓冲字符输入流
//BufferedWriter 缓冲字符输出流
//可以以行为单位进行输入、输出
缓冲字符输出流
public static void main(String[] args) {
File f = new File("word.txt");//创建一个文件
FileWriter fw = null;
BufferedWriter bw =null;
try {
fw = new FileWriter(f);
bw = new BufferedWriter(fw);//将文件字符输出流包装成缓冲字符流
String str1= "世界这么大";
String str2 = "我想去看看";
bw.write(str1);//第一行数据
bw.newLine();//创建一个新行
bw.write(str2);//第二行数据
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//要注意流的关闭顺序,先创建的后关闭
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fw !=null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
*/
缓冲字符输入流
public static void main(String[] args) {
File f = new File("word.txt");//创建一个文件
FileReader fr =null;
BufferedReader br = null;
try {
fr = new FileReader(f);
br = new BufferedReader(fr);//将文件字符输入流包装成缓冲字符输入流
String tmp = null;
int i = 1;//计数器
while( (tmp = br.readLine()) !=null) { //循环读取文件中的内容
System.out.println("第"+i+"行:"+tmp);
i++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (br!=null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fr!=null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}