五种复制文本文件的方式
package day04;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FiveCopyTest {
public static void main(String[] args) throws IOException {
String srcFileName = "Ping_Wei.java";
String destFileName = "g.txt";
method1(srcFileName,destFileName);
method2(srcFileName,destFileName);
method3(srcFileName,destFileName);
method4(srcFileName,destFileName);
method5(srcFileName,destFileName);
}
//5.缓冲流一次读写一个字符串
public static void method5(String srcFileName, String destFileName) throws IOException{
//创建输入缓冲流对象
BufferedReader br=new BufferedReader(new FileReader(srcFileName));
//创建输出缓冲流对象、
BufferedWriter bw = new BufferedWriter(new FileWriter(destFileName));
//读写数据一次一个字符串
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//4.缓冲流一次读写一个字符数组
public static void method4(String srcFileName, String destFileName) throws IOException{
//创建输入缓冲流对象
BufferedReader br=new BufferedReader(new FileReader(srcFileName));
//创建输出缓冲流对象、
BufferedWriter bw = new BufferedWriter(new FileWriter(destFileName));
//读写数据一次一个字符数组
char[] chs =new char[1024];
int len;
while((len=br.read(chs))!=-1){
bw.write(chs,0,len);
}
bw.close();
br.close();
}
//3.缓冲流一次读写一个字符
public static void method3(String srcFileName, String destFileName) throws IOException{
//创建输入缓冲流对象
BufferedReader br=new BufferedReader(new FileReader(srcFileName));
//创建输出缓冲流对象、
BufferedWriter bw = new BufferedWriter(new FileWriter(destFileName));
//读写数据一次一个字符
int ch;
while((ch=br.read())!=-1){
bw.write(ch);
}
bw.close();
br.close();
}
//2.基本流一次一个字符数组
public static void method2(String srcFileName, String destFileName) throws IOException{
//创建输入流对象
FileReader fr = new FileReader(srcFileName);
//创建输出流对象
FileWriter fw = new FileWriter(destFileName);
//读写数据一次一个字符数组
char[] chs =new char[1024];
int len;
while((len=fr.read(chs))!=-1){
fw.write(chs);
}
fw.close();
fr.close();
}
//1.基本流一次读写一个字符
public static void method1(String srcFileName, String destFileName) throws IOException{
//创建输入流对象
FileReader fr = new FileReader(srcFileName);
//创建输出流对象
FileWriter fw = new FileWriter(destFileName);
//读写数据,一次一个字符
int ch;
while((ch=fr.read())!=-1){
fw.write(ch);
}
//释放资源
fr.close();
fw.close();
}
}