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

📄 fileutils.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
字号:
package com.trulytech.mantis.util;

import java.util.ArrayList;
import java.io.File;
import java.io.IOException;

/**
 *
 * <p>Title: Mantis</p>
 *
 * <p>Description: 文件处理类</p>
 *
 * <p>Copyright: Copyright (c) 2002</p>
 *
 * <p>Company: </p>
 *
 * @author Wang Xian
 * @version 1.0
 */
public class FileUtils {

  /**
   * 获得指定路径下的所有文件名(包含全路径)
   * @param path String 指定目录
   * @return ArrayList 所有文件名(包含全路径)
   * @throws IOException
   */
  public static ArrayList getAllFiles(String path) throws IOException {

    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);
      //如果是目录
      if (tempfile.isDirectory()) {
        ArrayList arr = getAllFiles(tempfile.getPath());
        Ret.addAll(arr);
        arr.clear();
        arr = null;
      }
      else { //如果不是
        Ret.add(tempfile.getAbsolutePath());

      }

    }
    return Ret;
  }

  /**
   * 获得指定路径下的所有目录(包含全路径)
   * @param path String 指定目录
   * @return ArrayList 所有目录(包含全路径)
   * @throws IOException
   */
  public static ArrayList getAllDir(String path) throws IOException {
    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);

      if (tempfile.isDirectory()) {
        Ret.add(tempfile.getAbsolutePath());
        ArrayList arr = getAllDir(tempfile.getPath());
        Ret.addAll(arr);
        arr.clear();
        arr = null;

      }

    }
    return Ret;

  }

  /**
   * 获得指定路径下指定扩展名的所有文件(包含全路径)
   * @param path String 指定路径
   * @param Ext String 扩展名
   * @return ArrayList 所有文件(包含全路径)
   * @throws IOException
   */
  public static ArrayList getAllFiles(String path, String Ext) throws
      IOException {

    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);

      if (tempfile.isDirectory()) {
        ArrayList arr = getAllFiles(tempfile.getPath(), Ext);
        Ret.addAll(arr);
        arr.clear();
        arr = null;
      }
      else {
        if (getFileExt(tempfile.getAbsolutePath()).equalsIgnoreCase(Ext))
          Ret.add(tempfile.getAbsolutePath());

      }

    }
    return Ret;
  }

  /**
   * 获得指定路径下的文件列表(包含全路径),仅包含一级目录
   * @param path String 指定路径
   * @return ArrayList 文件名
   * @throws IOException
   */
  public static ArrayList getFiles(String path) throws IOException {

    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);

      if (!tempfile.isDirectory()) {
        Ret.add(tempfile.getAbsolutePath());

      }

    }
    return Ret;
  }

  /**
   * 获得指定路径下的子目录(包含全路径),仅包含一级目录
   * @param path String 指定路径
   * @return ArrayList 子目录
   * @throws IOException
   */
  public static ArrayList getDir(String path) throws IOException {

    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);

      if (tempfile.isDirectory()) {
        Ret.add(tempfile.getAbsolutePath());

      }

    }
    return Ret;
  }

  /**
   * 获得指定路径下指定扩展名的文件(包含全路径),仅包含一级目录
   * @param path String 指定路径
   * @param Ext String 扩展名
   * @return ArrayList 文件名
   * @throws IOException
   */
  public static ArrayList getFiles(String path, String Ext) throws IOException {

    File xmlfile = new File(path);
    ArrayList Ret = new ArrayList();
    String[] listFile = xmlfile.list();

    for (int i = 0; i < listFile.length; i++) {
      File tempfile = new File(path + System.getProperty("file.separator") +
                               listFile[i]);

      if (!tempfile.isDirectory()) {
        if (getFileExt(tempfile.getAbsolutePath()).equalsIgnoreCase(Ext))
          Ret.add(tempfile.getAbsolutePath());

      }

    }
    return Ret;
  }

  /**
   * 获得文件的扩展名
   * @param FileName String 文件名
   * @return String 扩展名,如果没扩展名,则返回空字符串
   */
  public static String getFileExt(String FileName) {
    int i = FileName.lastIndexOf(".");
    if (i > 0 && i < FileName.length() - 1)
      return FileName.substring(i + 1, FileName.length());
    else
      return "";
  }

}

⌨️ 快捷键说明

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