⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filecopy.java

📁 DATE_FORMAT时间Sql比较 for循环+hashmap
💻 JAVA
字号:
import java.io.*;
import java.text.*;
import java.util.*;
/**
 * 
 * <p>Title: IAdapter for Java </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author taoz
 * @version 2.0
 */
public class FileCopy {
  /**
   *  文件复制函数,参数是两个全路径文件名,如果发生错误,将抛出FileCopyException异常
   * @param source_name String
   * @param dest_name String
   * @param type int
   * 目标文件如果存在 参数1: 覆盖 ,2:生成一个新的文件, 3:不重新生成
   * @throws IOException
   * @return int
   */
  public static int copy(String source_name, String dest_name, int type) throws
      IOException {
    File source_file = new File(source_name);
    File dest_file = new File(dest_name);
    String destination_fileName = dest_name.substring(0,
        dest_name.indexOf(dest_file.getName())) + source_file.getName();
    File destination_file = new File(destination_fileName);
    FileInputStream source = null;
    FileOutputStream destination = null;
    byte[] buffer;
    int bytes_read;
    int result = 0;
    try {
      if (!source_file.exists() || !source_file.isFile()) {
        throw new FileCopyException("FileCopy: no such source file: " +
                                    source_name);
      }
      if (!source_file.canRead()) {
        throw new FileCopyException("FileCopy: source file " +
                                    "is unreadable: " + source_name);
      }

      if (destination_file.exists()) {
        if (destination_file.isFile()) {
          //目标文件 参数1 覆盖 2生成一个新的文件 3不重新生成
          //destination_file.delete();
          if (type == 1) {
            destination_file.delete();
            result = 1;
          }
          else if (type == 2) {
            String NewDesc_Name = dest_name.substring(0, dest_name.length() - 4);
            String EndName = dest_name.substring( (dest_name.length() - 3));
            NewDesc_Name += new java.text.SimpleDateFormat("yyyymmddssSSS").
                format(new Date()) + "." + EndName;
            destination_file = new File(NewDesc_Name);
            result = 2;
          }
          else {
            result = 3;
            return result;
          }
        }
        else {
          throw new FileCopyException("FileCopy: destination "
                                      + "is not a file: " + dest_name);
        }
      }
      else {
        File parentdir = parent(destination_file);
        if (!parentdir.exists()) {
          throw new FileCopyException("FileCopy: destination "
                                      + "directory doesn't exist: " +
                                      dest_name);
        }
        if (!parentdir.canWrite()) {
          throw new FileCopyException("FileCopy: destination "
                                      + "directory is unwriteable: " +
                                      dest_name);
        }
      }

      // If we've gotten this far, then everything is okay; we can
      // copy the file.
      source = new FileInputStream(source_file);
      destination = new FileOutputStream(destination_file);
      buffer = new byte[1024];
      while (true) {
        bytes_read = source.read(buffer);
        if (bytes_read == -1) {
          break;
        }
        destination.write(buffer, 0, bytes_read);
      }
    } //if end
    // No matter what happens, always close any streams we've  opened.
    finally {
      if (source != null) {
        try {
          source.close();
        }
        catch (IOException e) {
          ;
        }
      }
      if (destination != null) {
        try {
          destination.close();
        }
        catch (IOException e) {
          ;
        }
      }
      //先copy ,后换名
      if (!dest_file.getPath().equals(destination_file.getPath()) &&
          dest_file.exists()) {
        dest_file.delete();
      }
      destination_file.renameTo(dest_file);
      return result;
    }
  }

  // File.getParent() can return null when the file is specified without
  // a directory or is in the root directory.
  // This method handles those cases.
  private static File parent(File f) {
    String dirname = f.getParent();
    if (dirname == null) {
      if (f.isAbsolute()) {
        return new File(File.separator);
      }
      else {
        return new File(System.getProperty("user.dir"));
      }
    }
    return new File(dirname);
  }
}

class FileCopyException
    extends IOException {
  public FileCopyException(String msg) {
    super(msg);
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -