常用目录文件操作类

来源:互联网 发布:知乎 欧猪四国 编辑:程序博客网 时间:2024/05/29 09:06

package com.wen;

import java.io.*;

/**
 * <p>Title:常用目录文件操作类 </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author explorer
 * @version 1.0
 */

public class FileUtils {
  public FileUtils() {
  }

  public static void copy(File src, File dest, boolean cover) throws
      IOException {
    FileInputStream fis = new FileInputStream(src);
    FileOutputStream fos = new FileOutputStream(dest);
    byte buf[] = new byte[1024];
    int i;
    try {
      while ( (i = fis.read(buf)) >= 0) {
        fos.write(buf, 0, i);
      }
    }
    finally {
      try {
        fis.close();
      }
      catch (Exception exception1) {
      }

      try {
        fos.close();
      }
      catch (Exception exception2) {
      }
    }
  }

  public static void copy(String src, String dest, boolean cover) throws
      IOException {
    File fsrc = new File(src);
    File fdest = new File(dest);
    copy(fsrc, fdest, cover);
  }

  public static void copytree(File src, File dest, boolean cover) throws
      IOException {
    if (src.isFile()) {
      copy(src, dest, cover);
    }
    else {
      File children[] = src.listFiles();
      for (int i = 0; i < children.length; i++) {
        File f = new File(dest, children[i].getName());
        if (children[i].isDirectory()) {
          f.mkdirs();
          copytree(children[i], f, cover);
        }
        else {
          copy(children[i], f, cover);
        }
      }
    }
  }

  public static void copytree(String src, String dest, boolean cover) throws
      IOException {
    File fsrc = new File(src);
    File fdest = new File(dest);
    copytree(fsrc, fdest, cover);
  }

  public static void movetree(File src, File dest, boolean cover) throws
      IOException {
    copytree(src, dest, cover);
    deltree(src);
  }

  public static void deltree(File f) {
    File children[] = f.listFiles();
    if (children != null && children.length != 0) {
      for (int i = 0; i < children.length; i++) {
        deltree(children[i]);
      }
    }
    f.delete();
  }

  public static void deltree(String path) {
    File f = new File(path);
    deltree(f);
  }

  public static void move(String src, String dest, boolean cover) throws
      IOException {
    File fsrc = new File(src);
    File fdest = new File(dest);
    copy(fsrc, fdest, cover);
    fsrc.delete();
  }

  public static boolean find(File root, FileFilter filter) {
    if (filter.accept(root)) {
      return true;
    }

    File children[] = root.listFiles();
    if (children == null || children.length == 0) {
      return false;
    }

    for (int i = 0; i < children.length; i++) {
      if (find(children[i], filter)) {
        return true;
      }
    }

    return false;
  }

  public static boolean contains(File file, String suffix) {
    if (!file.exists() || !file.isDirectory()) {
      return false;
    }
    File children[] = file.listFiles();
    for (int i = 0; i < children.length; i++) {
      if (children[i].isFile() && children[i].getName().endsWith(suffix)) {
        return true;
      }
    }

    return false;
  }

  //读取单行文件内容
  public static String getFline(String filePath) {
    String fc = null;

    try {
      BufferedReader f = new BufferedReader(new FileReader(filePath));
      fc = f.readLine();
      f.close();
    }
    catch (IOException e) {
      System.out.println("readLine problem, terminating.");
    }

    return fc;
  }

  //读取单行文件内容并加一写入返回
  public static String getIline(String filePath) {
    String fc = null;

    try {
      BufferedReader f = new BufferedReader(new FileReader(filePath));
      fc = f.readLine();
      long l = Long.parseLong(fc);
      l++;
      fc = String.valueOf(l);
      f.close();

      BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
      bw.write(fc + "/n");
      bw.flush();
      bw.close();
    }
    catch (IOException e) {
      System.out.println("readLine problem, terminating.");
    }

    return fc;
  }

  public static String getFileExt(String fileName) {
    String ext = "";
    int dot = fileName.lastIndexOf(".");
    if (dot != -1) {
      ext = fileName.substring(dot + 1);
    }
    return ext;
  }

  public static String getFullFileName(String filePath) {
    String fileName = filePath;
    int i = filePath.lastIndexOf("/");
    if (i != -1) {
      fileName = filePath.substring(i + 1);
    }
    return fileName;
  }

  public static String getFileName(String filePath) {
    String fileName = filePath;
    int i = filePath.lastIndexOf("/");
    int j = filePath.lastIndexOf(".");
    if (i != -1) {
      fileName = filePath.substring(i + 1, j);
    }
    return fileName;
  }

  public static String getPath(String filePath) {
    String path = "";
    int i = filePath.lastIndexOf("/");
    if (i != -1) {
      path = filePath.substring(0, i);
    }
    return path;
  }

  public static boolean WriteTo(String path, String info) {
    try {
      File f = new File(path);
      PrintWriter out = new PrintWriter(new FileWriter(f));
      out.print(info);
      out.close();
      return true;
    }
    catch (IOException e) {
      return false;
    }
  }

  //追加字符串
  public static boolean Append(String path, String info) {
    try {
      File f = new File(path);
      PrintWriter out;
      FileWriter theFile;
      if (f.exists()) {
        theFile = new FileWriter(path, true);
        out = new PrintWriter(theFile);
      }
      else {
        theFile = new FileWriter(f);
        out = new PrintWriter(theFile);
      }
      out.print(info + "/n");
      out.close();
      theFile.close();
      return true;
    }
    catch (IOException e) {
      return false;
    }
  }

  public static String getFileContent(File srcfile) {
    String strReturn = "";

    try {
      BufferedInputStream buff = new BufferedInputStream(new FileInputStream(
          srcfile));

      int in = 0;
      do {
        in = buff.read();
        if (in != -1) {
          strReturn += (char) in;
        }
      }
      while (in != -1);

      return strReturn;
    }
    catch (Exception e) {
      return strReturn;
    }
  }

  public static String getFileContent(String fpath) {
    File srcfile = new File(fpath);
    return getFileContent(srcfile);
  }

  public static boolean isType(String ext, String ptype) {
    if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("gif") ||
        ext.equals("png") || ext.equals("bmp")) {
      if (ptype.equals("pic")) {
        return true;
      }
    }
    else if (ext.equals("mid") || ext.equals("amr") || ext.equals("wma")) {
      if (ptype.equals("ring")) {
        return true;
      }
    }
    else if (ext.equals("htm") || ext.equals("html")) {
      if (ptype.equals("webpage")) {
        return true;
      }
    }
    else if (ext.equals("css")) {
      if (ptype.equals("css")) {
        return true;
      }
    }
    else if (ext.equals("htt") || ext.equals("ini") || ext.equals("inc")) {
      if (ptype.equals("htt")) {
        return true;
      }
    }
    else if (ext.equals("js") || ext.equals("htc")) {
      if (ptype.equals("js")) {
        return true;
      }
    }
    return false;
  }
}