public class test {
public static void main(String[] args) throws IOException{
Method1("a.txt","C:\\Users\\29265\\Desktop\\1.txt");
Method2("a.txt","C:\\Users\\29265\\Desktop\\2.txt");
Method3("a.txt","C:\\Users\\29265\\Desktop\\3.txt");
Method4("a.txt","C:\\Users\\29265\\Desktop\\4.txt");
Method5("a.txt","C:\\Users\\29265\\Desktop\\5.txt");
}
private static void Method5(String string, String string2) throws IOException {
// TODO Auto-generated method stub
//字符缓冲流的特殊功能
BufferedReader br=new BufferedReader(new FileReader(string));
BufferedWriter bw=new BufferedWriter(new FileWriter(string2));
String len;
while((len=br.readLine())!=null){
bw.write(len);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
private static void Method4(String string, String string2) throws IOException {
// TODO Auto-generated method stub
//字符缓冲流一次写一个字符数组
BufferedReader br=new BufferedReader(new FileReader(string));
BufferedWriter bw=new BufferedWriter(new FileWriter(string2));
int len;
char[] ch=new char[1024];
while((len=br.read(ch))!=-1){
bw.write(ch,0,len);
}
br.close();
bw.close();
}
private static void Method3(String string, String string2) throws IOException {
// TODO Auto-generated method stub
//字符缓冲流一次写一个字符
BufferedReader br=new BufferedReader(new FileReader(string));
BufferedWriter bw=new BufferedWriter(new FileWriter(string2));
int len;
while((len=br.read())!=-1){
bw.write(len);
}
br.close();
bw.close();
}
private static void Method1(String string, String string2) throws IOException {
// TODO Auto-generated method stub
//字符流一次写一个字符
FileReader fr=new FileReader(string);
FileWriter fw=new FileWriter(string2);
int len;
while((len=fr.read())!=-1){
fw.write(len);
}
fr.close();
fw.close();
}
private static void Method2(String string, String string2) throws IOException {
// TODO Auto-generated method stub
//字符流一次写一个字符数组
FileReader fr=new FileReader(string);
FileWriter fw=new FileWriter(string2);
int len;
char[] ch=new char[1024];
while((len=fr.read(ch))!=-1){
fw.write(ch,0,len);
}
fr.close();
fw.close();
}
}
字符流复制文本的5种方式总结
最新推荐文章于 2021-09-19 20:27:31 发布