编写代码实现文件的复制功能
import java.io.*;
public class copyFile {
public void copy(String oldPath,String newPath){
try{
int bytesum=0;
int byteread=0;
File oldFile=new File(oldPath);//用来判断文件是否存在,通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
if(oldFile.exists()){
FileInputStream instream=new FileInputStream(oldPath);//<span style="font-size:10px;"><span style="font-family: Simsun; ">通过打开一个到实际文件的连接来创建一个 </span><code>FileInputStream</code></span>
FileOutputStream fs=new FileOutputStream(newPath);
byte[] buffer=new byte[1024];
int length;
while((byteread=instream.read(buffer))!=-1){//从此输入流中将最多 buffer.length 个字节的数据读入buffer 数组中,返回读入的数据流长度。
bytesum+=byteread;//字节数 文件的大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
instream.close();
}
}catch(Exception e){
System.out.println("复制文件出错!");
e.printStackTrace();
}
}
public static void main(String[] args){
new copyFile().copy("", "");
}
}