复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制

时间:2022-01-12 19:36:41
public class Test{    public static void main(String [] args)    {        Test t=new Test();        t.upload();    }    public void upload()    {        Scanner sc=new Scanner(System.in);        System.out.println("路径");        String str=sc.next();        File f=new File(str);                System.out.println("复制的位置:");        String di=sc.next();                FileInputStream fi=null;        FileOuputStream fo=null;                try{        fi=new FileInputStream (f);        fo=new FileOutputStream(di+File.separator+f.getName());//复制文件的名字                int num=0;        while((num=fi.read())!=-1)        {            fo.write(num);        }                System.out.println("成功");        }        catch(Exception e)        {                        e.printStackTrace();            System.out.println("失败");        }        finally        {            try{            fi.close();            fo.close();            }            catch(IOException e)            {                e.printStackTrace();            }        }                            }                }