* 缓冲流:提高读取、写入的效率 * * 字节缓冲输出流: * BufferedOutputStream * 字节缓冲输入流: * BufferedInputStream
package MONA.demo06_字节缓冲流;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Demo {
public static void main(String[] args) throws Exception {
//FileOutputStream fos = new FileOutputStream("3.txt");
//缓冲流
//第一种写法
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// bos.write(97);
// bos.close();
// fos.close();
// System.out.println("转换完成");
//第二种写法:FileOutputStream匿名对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("4.txt"));
bos.write(97);
bos.close();
}
}
字节缓冲输入流
import java.io.FileInputStream;
/**
* 字节缓冲输入流
* BufferedInputStream
*/
public class Demo02 {
public static void main(String[] args) throws Exception {
// FileInputStream fis = new FileInputStream("test.txt");
// BufferedInputStream bis = new BufferedInputStream(fis);
// System.out.println((char)bis.read());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.txt"));
System.out.println((char)bis.read());
}
}
字符缓冲输入流
package MONA.demo07_字符缓冲流;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
/**
* 字符缓冲输入流(缓冲字符输入流)
* BufferedReader
*/
public class Demo01 {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("1.txt");
fos.write(97);
FileReader fr = new FileReader("1.txt");
BufferedReader br = new BufferedReader(fr);
int c = br.read();
System.out.println((char) c);
}
}
字符缓冲输出流
package MONA.demo07_字符缓冲流;
import java.io.*;
/**
* 字符缓冲输出流
* BufferedWriter
*/
public class Demo02 {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("2.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("你好美女1");
//换行: 之前是/r/n 现在newLine()
bw.newLine();
bw.write("你好美女2");
bw.flush();
bw.close();
Reader fr = new FileReader("2.txt");
BufferedReader br = new BufferedReader(fr);
//缓冲流提供的方法,读取一行
String line = br.readLine();
System.out.println(line);
}
}
IO流拷贝数据效率对比
package MONA.demo09_缓冲流拷贝文件;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Demo01 {
public static void main(String[] args) throws Exception {
m1();
m2();
m3();
m4();
}
//缓冲流:一次读取一个字符
public static void m1() throws Exception {
long start = System.currentTimeMillis();
FileReader fr = new FileReader("1.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(new FileWriter("2.txt"));
int c = 0;
while ((c = br.read()) !=-1){
bw.write(c);
}
bw.close();
br.close();
fr.close();
long end = System.currentTimeMillis();
System.out.println("使用缓冲流复制完成"+((end - start)));
}
//默认方式:一次读取一个字符
public static void m2() throws Exception {
long start = System.currentTimeMillis();
FileReader fr = new FileReader("1.txt");
FileWriter bw = new FileWriter("3.txt");
int c = 0;
while ((c = fr.read()) !=-1){
bw.write(c);
}
bw.close();
fr.close();
long end = System.currentTimeMillis();
System.out.println("不使用缓冲流复制完成"+((end - start)));
}
//缓冲流:一次读取一个字符
public static void m3() throws Exception {
long start = System.currentTimeMillis();
FileReader fr = new FileReader("1.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(new FileWriter("45.txt"));
char[] chars = new char[1024];
int count = 0;
while ((count = br.read(chars)) !=-1){
bw.write(chars,0,count);
}
bw.close();
br.close();
fr.close();
long end = System.currentTimeMillis();
System.out.println("使用缓冲流:字符数组"+((end - start)));
}
//不缓冲流:一次读取一个字符
public static void m4() throws Exception {
long start = System.currentTimeMillis();
FileReader fr = new FileReader("1.txt");
FileWriter bw = new FileWriter("45.txt");
char[] chars = new char[1024];
int count = 0;
while ((count = fr.read(chars)) !=-1){
bw.write(chars,0,count);
}
bw.close();
fr.close();
long end = System.currentTimeMillis();
System.out.println("不使用缓冲流:字符数组"+((end - start)));
}
}