读书人

资料复制

发布时间: 2012-08-30 09:55:54 作者: rapoo

文件复制

 public static boolean copyFile(String srcFilePath, String dstFilePath)  {    File from_file = new File(srcFilePath);    File to_file = new File(dstFilePath);    if ((to_file.exists()) && (!(to_file.canWrite()))) {      throw new AfException("目标文件[" + srcFilePath + "已经存在,但不可写]");    }    FileInputStream from = null;    FileOutputStream to = null;    try {      from = new FileInputStream(from_file);      to = new FileOutputStream(to_file);      byte[] buffer = new byte[4096];      int bytes_read;      while ((bytes_read = from.read(buffer)) != -1)      {        to.write(buffer, 0, bytes_read); }    } catch (Exception ex) {      System.err.println("拷贝文件[" + srcFilePath + "]到[" + dstFilePath + "]异常:");      ex.printStackTrace();      return false;    } finally {      if (from != null)        try {          from.close();        }        catch (IOException localIOException2) {        }      if (to != null)        try {          to.close();        }        catch (IOException localIOException3) {        }    }    return true;  }

读书人网 >软件开发

热点推荐