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;
 } .net

 /**
  * 壓縮。
  *
  * @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) {
    }
   }
  }
 } ip

 /**
  * 遞歸壓縮文件或目錄
  *
  * @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); get

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

    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) {
    }
   }
  }
 } it

 /**
  * 複製單個文件
  *
  * @param oldPath
  *            String 原文件路徑 如:c:/fqf.txt
  * @param newPath
  *            String 複製後路徑 如:f:/fqf.txt
  * @return boolean
  */
 public void copyFile(String oldPath, String newPath){ io

  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;
  } class

 } file

 /**
  * 複製整個文件夾內容
  *
  * @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;  }

相關文章
相關標籤/搜索