您的位置:首页 > 其它

IO流复制文件11种方法

2016-08-17 23:16 381 查看

1.IO流复制文件方法汇总

①字节流复制文件(FileInputStream/FileOutputStream和BufferedInputSream/BufferedOutputStream)

②字符流复制文件(InputStreamReader/OutputStreamWriter、FileReader/FileWriter、BufferedReader/BufferedWriter)

BufferedReader/BufferedWriter字符缓冲区流有三种方法,多了一种可以直接读取一行,一次写入一个字符串。其它都是两种方法。

2.代码体现

package cn.jason04;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//InputStreamReader是FileReader的父亲,FileReader是InputStreamReader的简化版
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

//复制文件11种方法
public class Test01 {
public static void main(String[] args) throws IOException {
// 字节流
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("f.txt");
// 字节缓冲区流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f.txt"));

// 字符流
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f.txt"));

FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("f.txt");
// 字符缓冲区流
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));

// 字节流方法
copyFiles1(fis, fos);
copyFiles2(fis, fos);

copyFiles3(bis, bos);
copyFiles4(bis, bos);

// 字符流读取方法
copyFiles5(isr, osw);
copyFiles6(isr, osw);

copyFiles7(fr, fw);
copyFiles8(fr, fw);

// 高效方法
copyFiles9(br, bw);
copyFiles10(br, bw);

// 特效方法
copyFiles11(br, bw);
}

// 字节读取方法
private static void copyFiles1(FileInputStream fis, FileOutputStream fos) throws IOException {
// 一次读取一个字节
int bys = 0;
while ((bys = fis.read()) != -1) {
fos.write(bys);
}
// 关闭流
fos.close();
fis.close();

}

private static void copyFiles2(FileInputStream fis, FileOutputStream fos) throws IOException {
// 一次读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
// 读一个字节数组
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 关闭流
fos.close();
fis.close();

}

private static void copyFiles3(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {
// 一次读取一个字节
int bys = 0;
while ((bys = bis.read()) != -1) {
bos.write(bys);
}
}

private static void copyFiles4(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {
// 一次读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
// 关闭流
bos.close();
bis.close();

}

// 字符读取方法
// 普通方法
// 方式1
public static void copyFiles5(InputStreamReader isr, OutputStreamWriter osw) throws IOException {
// 一次读取一个字符
// InputStreamReader是FileReader的父类
int ch = 0;
while ((ch = isr.read()) != -1) {
osw.write(ch);
osw.flush();
}

isr.close();
osw.close();
}

// 方式2
public static void copyFiles6(InputStreamReader isr, OutputStreamWriter osw) throws IOException {
// 一次读取一个字符数组
char[] ch = new char[1024];
int len = 0;
while ((len = isr.read(ch)) != -1) {
osw.write(ch, 0, len);
osw.flush();
}

isr.close();
osw.close();
}

// 方法3
public static void copyFiles7(FileReader fr, FileWriter fw) throws IOException {
// 普通方法一次读写一个字符
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
fw.flush();
}

fw.close();
fr.close();
}

// 方法4
public static void copyFiles8(FileReader fr, FileWriter fw) throws IOException {

// 普通方法一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
fw.flush();
}

fw.close();
fr.close();
}

// 高效方法
// 方法5
public static void copyFiles9(BufferedReader br, BufferedWriter bw) throws IOException {
// 高效一次读取一个字符
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
bw.flush();
}

br.close();
bw.close();
}

// 方法6
public static void copyFiles10(BufferedReader br, BufferedWriter bw) throws IOException {
// 高效一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}

br.close();
bw.close();
}

// 特效方法
// 方法7
public static void copyFiles11(BufferedReader br, BufferedWriter bw) throws IOException {
// 特效方式
String string = null;
// 一次读写一行
while ((string = br.readLine()) != null) {
bw.write(string);
bw.newLine();
bw.flush();
}
// 关闭输入输出流
br.close();
bw.close();
}

}


3.归纳总结

1.IO流之间关系要明确,那些流有特殊用法要明确,基本流一定要掌握。

2.读文件的时候,文件目录一定要存在,否则会抛出异常,文件目录不存在。

3.写文件的时候,会自动创建文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息