java用Fileinputstream类实现简单的文件复制

package com.test2;

import java.io.FileInputStream;
import java.io.FileOutputStream;

//功能:文件的复制

public class moveFile {


//写一个move方法,两个参数代表的是开始路径和复制的路径
    public void move(String start,String end)
    {
    FileInputStream fp=null;
    FileOutputStream op=null;
   
    try {
fp=new FileInputStream(start);
op=new FileOutputStream(end);

//创建一个Byte变量n,存放读入的字节
int n;
byte bt[]=new byte[1024];
//循环读数
while((n=fp.read())!=-1)
{
//将读到的数据输出
op.write(bt);


}
} catch (Exception e) {
e.printStackTrace();
}finally{

try {
//尝试关闭文件
fp.close();
op.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
   
    }


public static void main(String[] args) {
//创建一个新的moveFile类对象
moveFile a=new moveFile();
//调用里面的复制方法
a.move("d:\\a.jpg", "e:\\movea.jpg");

}

}

你可能感兴趣的:(FileInputStream)