如给出:“E:/eclipse3.02/2024/200386-4917.jpg”
怎样将该文件拷贝到另外一个目录?
若子目录不存在自动创建。“F:/MYFILES/2024/200386-4917.jpg”

解决方案 »

  1.   

    FileInputStream fin = new FileInputStream(“E:\\\eclipse3.02\\2024\\200386-4917.jpg”
    ");
           File fcopy = new File("F:\\\MYFILES\\2024\\200386-4917.jpg");
           fcopy.createNewFile() ;
           
           FileOutputStream fout = new FileOutputStream(fcopy);
           
           int b;
           while((b=fin.read())!=-1){
                  fout.write(b);注意文件分隔符
      

  2.   

    public  void CopyFile(String in , String out) throws Exception {
    File inf = new File(in);

    if(!inf.exists())
    return;
        File outf = new File(out);
        
        String outPath[] = out.split("/");
        String tmp =outPath[0];
        for(int i =1; i< outPath.length -1;i++)
        {   tmp = tmp +"/" + outPath[i];
         outf =new File(tmp +"/");
            if(!outf.exists())
        {
          outf.mkdir();
        
        }
        
        }
        outf =new File(out);
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(inf);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     FileOutputStream fos = null;

     try {
    fos = new FileOutputStream(outf);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

     byte[] buf = new byte[1024];
     int i = 0;
     while((i=fis.read(buf))!=-1) {
       fos.write(buf, 0, i);
     }
     fis.close();
     fos.close();
      }
    //call :
        try {
    a.CopyFile("d:/aa/aa.bmp","d:/aa/bb/aa.bmp");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }