import java.io.*;
//我主要想问的是一个类的构造函数的传入参数能不能是File对象,如果能,那么怎样接受!
class CopyFile{
//首先要定义接受的参数
private File f1;
private File f2;
//构造函数初始化
public CopyFile(File f1,File f2){
this.f1 = f1;
this.f2 = f2;
}
//复制的方法,因为两个文件都是成员变量,可以直接调用
public void doCopy(){
BufferedInputStream bis = null;
BufferedOutputStream bos =null;
try {
bis = new BufferedInputStream(new FileInputStream(this.f1));
bos = new BufferedOutputStream(new FileOutputStream(this.f2));
int by = 0;
while((by=bis.read()) != -1){
bos.write(by);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File f1 = new File("hello.html");
File f2 = new File("hello1.html");
CopyFile d = new CopyFile(f1,f2);
d.doCopy();
}
}
这个是否是你想要的? |