黑马程序员技术交流社区
标题:
IO中的四种复制文件的方法
[打印本页]
作者:
虎鹏
时间:
2015-5-14 22:47
标题:
IO中的四种复制文件的方法
package cn.IOtest_4method;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FourMethodAvi {
public static void main(String[] args) throws IOException {
//method1("20.14_IO.avi", "Copy1.avi");
//method2("20.14_IO.avi", "Copy3.avi");
//method3("20.14_IO.avi", "Copy3.avi");
method4("20.14_IO.avi", "Copy4.avi");
}
private static void method4(String src, String dest) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
byte[] bys = new byte[1024];
int len = -1;
while((len = bis.read(bys)) != -1){
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
private static void method3(String src, String dest) throws IOException {
FileInputStream bis = new FileInputStream(src);
FileOutputStream bos = new FileOutputStream(dest);
byte[] bys = new byte[1024];
int len = -1;
while((len = bis.read(bys)) != -1){
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
private static void method2(String src, String dest) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
int ch = -1;
while((ch = bis.read()) != -1){
bos.write(ch);
}
bis.close();
bos.close();
}
public static void method1(String src , String dest) throws IOException{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
int ch = -1;
while((ch = fis.read()) != -1){
fos.write(ch);
}
fos.close();
fis.close();
}
}
复制代码
作者:
xiaodaodan
时间:
2015-5-16 22:26
还有字符流处理,特殊处理等等,慢慢添加
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2