IO流分类
System.currentTimeMillis()
以毫秒为单位返回当前时间long start = System.currentTimeMillis();
//调用复制方法
emthod01();
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
FileOutputStream
和FileInputStream
public static void emthod01() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
共耗时140475毫秒
竟然用了两分多钟,我一度以为我写错了
public static void emthod02() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
共耗时211毫秒
BufferedOutputStream
和BufferedInputStream
public static void emthod03() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
共耗时349毫秒
public static void emthod04() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
}
共耗时43毫秒
//生成一个文件
public static void emthod() throws IOException {
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw.txt"));
int i = 0;
while (i < 1000000) {
fw.write("好");
i++;
}
fw.close();
}
FileInputStream
和FileOutputStream
public static void emthod1() throws IOException {
FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
共耗时16273毫秒
public static void emthod2() throws IOException {
FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
共耗时31毫秒
BufferedInputStream
和BufferedOutputStream
bos
和bis
,再关闭内层流fis
和fos
public static void emthod3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
bos.flush();
}
bis.close();
bos.close();
}
共耗时16273毫秒
public static void emthod4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));
int len = 0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
bos.flush();
}
bis.close();
bos.close();
}
共耗时28毫秒
FileReader
和 FileWriter
注意:FileReader
和 FileWriter
只能处理字符流
一次一个字符
public static void emthod5() throws IOException {
FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
int by;
while ((by = fr.read()) != -1) {
fw.write(by);
}
fw.close();
fr.close();
}
共耗时122毫秒
public static void emthod6() throws IOException {
FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
int len = 0;
char[] chars = new char[1024];
while ((len = fr.read(chars)) != -1) {
fw.write(chars, 0, len);
}
fw.close();
fr.close();
}
共耗时66毫秒
BufferedReader
和BufferedWriter
BufferedReader
除了继承了Reader类的方法之外,有自己的独有方法:
String readLine()
:读取一行数据,终止符号:
换行(\n
),回车(\r
),\r\n
(windows)
它的返回值包含改行的字符串,不包含任何终止符号,如果到达流末尾,返回null
public static void emthod7() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
int by;
while ((by = br.read()) != -1) {
bw.write(by);
}
//释放资源
br.close();
bw.close();
}
共耗时80毫秒
public static void emthod8() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1) {
new String(chars, 0, len);
}
//释放资源
br.close();
bw.close();
}
共耗时49毫秒
public static void emthod9() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
String data;
while ((data = br.readLine()) != null) {
bw.write(data);
// 换行
bw.newLine();
}
//释放资源
br.close();
bw.close();
}
共耗时74毫秒
看到这里了,点个关注再走吧