コード例 #1
0
ファイル: FileUtil.java プロジェクト: netsail/hutool
 /**
  * 将流的内容写入文件<br>
  *
  * @param dest 目标文件
  * @param in 输入流
  * @throws IOException
  */
 public static void writeStream(File dest, InputStream in) throws IOException {
   FileOutputStream out = null;
   try {
     out = new FileOutputStream(dest);
     IoUtil.copy(in, out);
   } finally {
     close(out);
   }
 }
コード例 #2
0
ファイル: FileUtil.java プロジェクト: netsail/hutool
  /**
   * 复制文件<br>
   * 如果目标文件为目录,则将源文件以相同文件名拷贝到目标目录
   *
   * @param src 源文件
   * @param dest 目标文件或目录
   * @param isOverride 是否覆盖目标文件
   * @throws IOException
   */
  public static void copy(File src, File dest, boolean isOverride) throws IOException {
    // check
    if (!src.exists()) {
      throw new FileNotFoundException("File not exist: " + src);
    }
    if (!src.isFile()) {
      throw new IOException("Not a file:" + src);
    }
    if (equals(src, dest)) {
      throw new IOException("Files '" + src + "' and '" + dest + "' are equal");
    }

    if (dest.exists()) {
      if (dest.isDirectory()) {
        dest = new File(dest, src.getName());
      }
      if (dest.exists() && !isOverride) {
        throw new IOException("File already exist: " + dest);
      }
    }

    // do copy file
    FileInputStream input = new FileInputStream(src);
    FileOutputStream output = new FileOutputStream(dest);
    try {
      IoUtil.copy(input, output);
    } finally {
      close(output);
      close(input);
    }

    if (src.length() != dest.length()) {
      throw new IOException(
          "Copy file failed of '" + src + "' to '" + dest + "' due to different sizes");
    }
  }