字节流的读取方法适合读取图片、音频、视频等文件。
字符流的读取方式适合读取大型文本。
我们来对比彼此间的读写效率来进行比较,分别以1.55MB的图片和3M的《剑来》两张文章作位读写对象。
字节流
1、字节流:FileInputStream、FileOutputStream(逐字节读取)
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("kf.jpg");
FileOutputStream fos = new FileOutputStream("kf1.jpg");
//读写操作:
long s1 = System.currentTimeMillis();
int len;
while ((len = fis.read())!=-1){
fos.write(len);
}
fis.close();
fos.close();
long e1 = System.currentTimeMillis();
System.out.println((e1-s1)+"毫秒");
}
耗时:
2、用缓冲流来优化字节流:BufferedFileInputStream、BufferedFileOutputStream(逐字节读取)
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("kf.jpg");
FileOutputStream fos = new FileOutputStream("kf1.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//读写操作:
long s1 = System.currentTimeMillis();
int len;
while ((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
fos.close();
bis.close();
fis.close();
long e1 = System.currentTimeMillis();
System.out.println((e1-s1)+"毫秒");
}
结果21秒:速度提升了300倍,可见缓冲流的的强大之处。
3、给字节流加上缓冲数组:一次读取一个数组的长度
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("kf.jpg");
FileOutputStream fos = new FileOutputStream("kf1.jpg");
//读写操作:
long s1 = System.currentTimeMillis();
int len;
byte[] b = new byte[1024];
while ((len = fis.read(b))!=-1){
fos.write(b);
}
fis.close();
fos.close();
long e1 = System.currentTimeMillis();
System.out.println((e1-s1)+"毫秒");
}
速度继续提升,结果1.5M的图片只需要10毫秒
4、给缓冲流加上缓冲数组
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("kf.jpg");
FileOutputStream fos = new FileOutputStream("kf1.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//读写操作:
long s1 = System.currentTimeMillis();
int len;
byte[] b = new byte[1024];
while ((len = bis.read(b))!=-1){
bos.write(b,0,len);
}
fos.close();
bos.close();
fis.close();
bis.close();
long e1 = System.currentTimeMillis();
System.out.println((e1-s1)+"毫秒");
}
此时缓冲流继续加强,比加上缓冲数组的字节流速度再次提升了5倍,这若是个浩大的工程节约的可不止这一点点速度。
字符流:
1、FileWriter、FileReader(按字符读取!)
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("1234.txt");
FileReader fr = new FileReader("123.txt");
long s2 = System.currentTimeMillis();
int len;
while((len = fr.read()) != -1){
fw.write(len);
}
fw.close();
fr.close();
long e2 = System.currentTimeMillis();
System.out.println((e2-s2)+"毫秒");
}
3M的文本文件耗时:
2、给字节流加上数组
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("1234.txt");
FileReader fr = new FileReader("123.txt");
long s2 = System.currentTimeMillis();
char[] c = new char[1024];
while((fr.read(c)) != -1){
fw.write(c);
}
fw.close();
fr.close();
long e2 = System.currentTimeMillis();
System.out.println((e2-s2)+"毫秒");
}
结果:
3、使用缓冲流的缓冲数组读取。
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("1234.txt");
FileReader fr = new FileReader("123.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
long s2 = System.currentTimeMillis();
char[] c = new char[1024];
while((br.read(c)) != -1){
bw.write(c);
bw.newLine();
}
bw.flush();
fw.close();
bw.close();
fr.close();
br.close();
long e2 = System.currentTimeMillis();
System.out.println((e2-s2)+"毫秒");
}
结果:
字节流和字符流读同一个文件:
public static void main(String[] args) throws IOException {
//字节流
FileInputStream fis = new FileInputStream("kf.jpg");
FileOutputStream fos = new FileOutputStream("kf2.jpg");
//字符流
FileReader fr = new FileReader("kf.jpg");
FileWriter fw = new FileWriter("kf3.jpg");
//缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
//字节流读取图片
long s1 = System.currentTimeMillis();
int len;
byte[] b = new byte[1024];
while((len = bis.read(b))!= -1){//111毫秒
bos.write(b,0,len);
}
long e1 = System.currentTimeMillis();
System.out.println("字节流:"+(e1-s1)+"毫秒");
//字符流读图片
long s2 = System.currentTimeMillis();
char[] c = new char[1024];
while(br.read(c)!=-1){
bw.write(c);
}
long e2 = System.currentTimeMillis();
System.out.println("字符流"+(e2-s2)+"毫秒");
bw.flush();
bos.flush();
bos.close();
fos.close();
br.close();
bw.close();
fw.close();
fis.close();
fr.close();
bis.close();
}
结果:用字符流读取图片速度很慢!