使用Java的IO分为字节流的IO和字符流的IO,需要说明的是:
1.复制文本文件(也就是可以通过记事本打开查看的),可以使用字符流也可以使用字节流,推荐使用字符流;复制其他文件(不能用记事本打开查看的)只能使用字节流.
2.字节流和字符流的IO都有两种方式,普通的IO和缓冲区IO,缓冲区IO快于普通IO,推荐使用缓冲区IO.
3.以下案例中,前两个为字节流案例,后四个为字符流案例,字符流复制非常推荐使用copy6的方法
package cn.lg.fileCopy;
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;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* @author L
* @date 2017年3月14日 下午11:31:33
*
*/
public class FileCopyDemo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
copy6("a.txt", "b.txt");
long end = System.currentTimeMillis();
System.out.println("复制完成,使用: " + (end - start) + " 毫秒");
}
/**
* 字节流方式
*
* @param path1
* @param path2
* @throws IOException
*/
private static void copy1(String path1, String path2) throws IOException {
FileInputStream fis = new FileInputStream(path1);
FileOutputStream fos = new FileOutputStream(path2);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
/**
* 缓冲区字节流方式
*
* @param path1
* @param path2
* @throws IOException
*/
private static void copy2(String path1, String path2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));
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();
}
/**
* 字符流方式
*
* @param path1
* @param path2
* @throws IOException
*/
private static void copy3(String path1, String path2) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(path1));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path2));
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
osw.write(chs, 0, len);
osw.flush();
}
isr.close();
osw.close();
}
/**
* 文件字符流
*
* @param path1
* @param path2
* @throws IOException
*/
private static void copy4(String path1, String path2) throws IOException {
FileReader fr = new FileReader(path1);
FileWriter fw = new FileWriter(path2);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
fw.flush();
}
fr.close();
fw.close();
}
/**
* 缓冲区字符流
*
* @param path1
* @param path2
* @throws IOException
*/
private static void copy5(String path1, String path2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path1));
BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
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();
}
/**
* 使用缓冲区一行行的读文本,非常推荐使用!!!
* @param path1
* @param path2
* @throws IOException
*/
private static void copy6(String path1, String path2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path1));
BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
String line = br.readLine();
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}