- java IO流的分类:按照流向可分为输入流和输出流,按照数据类型可分为字节流和字符流。
- IO流的适用场景和效率:在处理音频,视频,图片这些文件时一般使用字节流,而使用缓冲字节流(BufferedInputStream和BufferedOutputStream)会比文件字节流(FileInputStream和FileOutputStream)的效率高;在处理文本文件时一般使用字符流,而使用缓冲字符流(BufferedReader和BufferedWriter)会比文件字符流(FileReader和FileWriter)的效率高。
下面用一张图来概括IO流体系:
用字节流复制视频并比较效率
package com.yixun.cn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class CopyPicture {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv"));
fos = new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv"));
byte[] b = new byte[64];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("文件字节流复制视频花费时间为:" + (endTime - startTime));
}
@Test
public void test() {
long startTime = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\mayu\\Desktop\\Wildlife.wmv")));
bos = new BufferedOutputStream(
new FileOutputStream(new File("C:\\Users\\mayu\\Desktop\\test\\Wildlife1.wmv")));
byte[] b = new byte[64];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("缓冲字节流复制视频花费时间为:" + (endTime - startTime));
}
}
5. 用字符流复制文本文件并比较效率
package com.yixun.cn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.junit.Test;
public class CopyText {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File(
"C:\\Users\\mayu\\Desktop\\xxx.txt"));
fw = new FileWriter(new File("C:\\Users\\mayu\\Desktop\\test\\xxx.txt"));
char[] c = new char[64];
int len = 0;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("文件字符流复制文本花费时间为:" + (endTime - startTime));
}
@Test
public void test() {
long startTime = System.currentTimeMillis();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"C:\\\\Users\\\\mayu\\\\Desktop\\\\xxx.txt"))));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
new File("C:\\Users\\mayu\\Desktop\\test\\x"))));
char[] c = new char[64];
int len = 0;
while ((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("缓冲字符流复制文本花费时间为:" + (endTime - startTime));
}
}