java文件操作源码

 // 创建文件夹
 private File createDir(String path) {
  File dirFile = null;
  try {
   dirFile = new File(path);
   if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
    dirFile.mkdirs();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return dirFile;
 }

 /**
  * 压缩。
  *
  * @param src
  *            源文件或者目录
  * @param dest
  *            压缩文件路径
  * @throws IOException
  */
 public void zip(String src, String dest) throws IOException {
  ZipOutputStream out = null;
  try {
   File outFile = new File(dest);
   out = new ZipOutputStream(outFile);
   File fileOrDirectory = new File(src);

   if (fileOrDirectory.isFile()) {
    zipFileOrDirectory(out, fileOrDirectory, "");
   } else {
    File[] entries = fileOrDirectory.listFiles();
    for (int i = 0; i < entries.length; i++) {
     // 递归压缩,更新curPaths
     zipFileOrDirectory(out, entries[i], "");
    }
   }

  } catch (IOException ex) {
   ex.printStackTrace();
  } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException ex) {
    }
   }
  }
 }

 /**
  * 递归压缩文件或目录
  *
  * @param out
  *            压缩输出流对象
  * @param fileOrDirectory
  *            要压缩的文件或目录对象
  * @param curPath
  *            当前压缩条目的路径,用于指定条目名称的前缀
  * @throws IOException
  */
 private void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory,
   String curPath) throws IOException {
  FileInputStream in = null;
  try {
   if (!fileOrDirectory.isDirectory()) {
    // 压缩文件
    byte[] buffer = new byte[4096];
    int bytes_read;
    in = new FileInputStream(fileOrDirectory);

    ZipEntry entry = new ZipEntry(curPath
      + fileOrDirectory.getName());
    out.putNextEntry(entry);

    while ((bytes_read = in.read(buffer)) != -1) {
     out.write(buffer, 0, bytes_read);
    }
    out.closeEntry();
   } else {
    // 压缩目录
    File[] entries = fileOrDirectory.listFiles();
    for (int i = 0; i < entries.length; i++) {
     // 递归压缩,更新curPaths
     zipFileOrDirectory(out, entries[i], curPath
       + fileOrDirectory.getName() + "/");
    }
   }
  } catch (IOException ex) {
   ex.printStackTrace();
   throw ex;
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException ex) {
    }
   }
  }
 }

 /**
  * 复制单个文件
  *
  * @param oldPath
  *            String 原文件路径 如:c:/fqf.txt
  * @param newPath
  *            String 复制后路径 如:f:/fqf.txt
  * @return boolean
  */
 public void copyFile(String oldPath, String newPath){

  int bytesum = 0;
  int byteread = 0;
  File oldfile = new File(oldPath);
  if (oldfile.exists()) { // 文件存在时
   try {
    InputStream inStream = new FileInputStream(oldPath); // 读入原文件
    FileOutputStream fs = new FileOutputStream(newPath);
    byte[] buffer = new byte[1444];
    int length;
    while ((byteread = inStream.read(buffer)) != -1) {
     bytesum += byteread; // 字节数 文件大小
     // System.out.println(bytesum);
     fs.write(buffer, 0, byteread);
    }
    inStream.close();
    fs.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } else {
   try {
    abort("没有这样的源文件: " + oldfile.getName());
   } catch (IOException e) {
    e.printStackTrace();
   }
   return;
  }

 }

 /**
  * 复制整个文件夹内容
  *
  * @param oldPath
  *            String 原文件路径 如:c:/fqf
  * @param newPath
  *            String 复制后路径 如:f:/fqf/ff
  * @return boolean
  */
 public void copyFolder(String oldPath, String newPath) {
  try {
   (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
   File a = new File(oldPath);
   String[] file = a.list();
   File temp = null;
   for (int i = 0; i < file.length; i++) {
    if (oldPath.endsWith(File.separator)) {
     temp = new File(oldPath + file[i]);
    } else {
     temp = new File(oldPath + File.separator + file[i]);
    }

    if (temp.isFile()) {
     FileInputStream input = new FileInputStream(temp);
     FileOutputStream output = new FileOutputStream(newPath
       + "/" + (temp.getName()).toString());
     byte[] b = new byte[1024 * 5];
     int len;
     while ((len = input.read(b)) != -1) {
      output.write(b, 0, len);
     }
     output.flush();
     output.close();
     input.close();
    }
    if (temp.isDirectory()) {// 如果是子文件夹
     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
    }
   }
  } catch (Exception e) {
   System.out.println("复制整个文件夹内容操作出错");
   e.printStackTrace();
  }

 }

 // 删除文件及文件夹
 private boolean delDir(File folder) {
  boolean result = false;
  try {
   String childs[] = folder.list();
   if (childs == null || childs.length <= 0) {
    if (folder.delete()) {
     result = true;
    }
   } else {
    for (int i = 0; i < childs.length; i++) {
     String childName = childs[i];
     String childPath = folder.getPath() + File.separator
       + childName;
     File filePath = new File(childPath);
     if (filePath.exists() && filePath.isFile()) {
      if (filePath.delete()) {
       result = true;
      } else {
       result = false;
       break;
      }
     } else if (filePath.exists() && filePath.isDirectory()) {
      if (delDir(filePath)) {
       result = true;
      } else {
       result = false;
       break;
      }
     }
    }
   }
   folder.delete();
  } catch (Exception e) {
   e.printStackTrace();
   result = false;
  }
  return result;
 }

你可能感兴趣的:(java文件操作源码)