package com.xst.IO;
//IO流9种拷贝方式
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class FileCopy {
public static void main(String[] args) {
//copy1(srcFile, descFile);
//copy2(srcFile2, descFile2);
//copy3(srcFile3, descFile3);
//copy4(srcFile4, descFile4);
//copy5(srcFile5, descFile5);
//copy6(srcFile6, descFile6);
//copy7(srcFile7, descFile7);
//copy8(srcFile8, descFile8);
//copy9(srcFile9, descFile9);
}
public static void copy1(String srcFile,String descFile){
// 第一种: 使用FileReader和FileWrite,一次读取一个字符
try {
FileReader fr=new FileReader(srcFile);
FileWriter fw=new FileWriter(descFile);
int ch=0;
while ((ch=fr.read())!=-1) {
fw.write(ch);
}
fw.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy2(String srcFile2,String descFile2) {
// 第二种: 使用FileReader和FileWrite,一次读取一个字符数组
try {
FileReader fr2=new FileReader(srcFile2);
FileWriter fw2=new FileWriter(descFile2);
int len=0;
char[] chs=new char[1024];
while ((len=fr2.read(chs))!=-1) {
fw2.write(chs, 0, len);
}
fw2.close();
fr2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy3(String srcFile3,String descFile3) {
// 第三种: 使用FileOutputStream和FileInputStream,一次读取一个字节
try {
FileInputStream fis=new FileInputStream(srcFile3);
FileOutputStream fos=new FileOutputStream(descFile3);
int ch=0;
while ((ch=fis.read())!=-1) {
fos.write(ch);
}
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy4(String srcFile4,String descFile4) {
// 第四种: 使用FileOutputStream和FileInputStream,一次读取一个字节数组
try {
FileInputStream fis2=new FileInputStream(srcFile4);
FileOutputStream fos2=new FileOutputStream(descFile4);
int len=0;
byte[] by=new byte[1024];
while ((len=fis2.read(by))!=-1) {
fos2.write(by, 0, len);
}
fos2.close();
fis2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy5(String srcFile5,String descFile5) {
// 第五种: 使用BufferedReader和BufferedWriter,一次读取一行
try {
BufferedReader br=new BufferedReader(new FileReader(srcFile5));
BufferedWriter bw=new BufferedWriter(new FileWriter(descFile5));
String line=null;
while ((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy6(String srcFile6,String descFile6) {
// 第六种: 使用高效缓冲流,BufferedInputStream和BufferedOutputStream,一次读取一个字节
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt"));
int ch=0;
while ((ch=bis.read())!=-1) {
bos.write(ch);
}
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy7(String srcFile7,String descFile7) {
// 第七种: 使用高效缓冲流,BufferedInputStream和BufferedOutputStream,一次读取一个字节数组
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt"));
int ch=0;
byte[] by=new byte[1024];
while ((ch=bis.read(by))!=-1) {
bos.write(by,0,ch);
bos.flush();
}
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy8(String srcFile8,String descFile8) {
// 第八种: 使用转换流 OutputStreamWrite和InputSreamReader,一次读取一个字符
try {
FileInputStream fis = new FileInputStream("D:\\a.txt");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream("D:\\b.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
int len=0;
while ((len=isr.read())!=-1) {
osw.write(len);
}
osw.close();
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy9(String srcFile9,String descFile9) {
// 第九种: 使用转换流 OutputStreamWrite和InputSreamReader,一次读取一个字符数组
try {
FileInputStream fis = new FileInputStream("D:\\a.txt");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream("D:\\b.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
int len=0;
char[] by = new char[1024];
while ((len = isr.read(by)) != -1) {
osw.write(by, 0, len);
}
isr.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
该博客介绍了Java中使用IO流进行文件拷贝的9种不同方式,包括使用BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter等类进行文件的复制操作,涵盖了从 FileInputStream 到 FileOutputStream 的多种组合。虽然示例代码没有展示具体实现,但可以看出作者意在详细解析各种拷贝策略及其异常处理。
1131

被折叠的 条评论
为什么被折叠?



