当前位置 : 主页 > 编程语言 > java >

java CS-SeachUtil

来源:互联网 收集:自由互联 发布时间:2021-06-28
SeachUtil.java package cn.huawei.com.CompressedSeacher.util;import cn.huawei.com.CompressedSeacher.view.com.impl.FileListTableImpl;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;i
SeachUtil.java
package cn.huawei.com.CompressedSeacher.util;
import cn.huawei.com.CompressedSeacher.view.com.impl.FileListTableImpl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * **
 *
 * @author l00358914
 */
public class SeachUtil {
    private static final SeachUtil instance = new SeachUtil();
    private SeachUtil() {
    }
    public static SeachUtil getInstance() {
        return instance;
    }
    /**
     * 将文本文件中的内容读入到buffer中
     *
     * @param buffer   buffer
     * @param filePath 文件路径
     * @throws IOException 异常
     * @author cn.outofmemory
     *
     */
    public static boolean isFileOkToAdd(String filePath, String[] conditions) throws IOException {
        //需要对文件路径进行判断,如果不存在,直接返回
        if (!new File(filePath).exists()) {
            return false;
        }
        InputStream is = new FileInputStream(filePath);
//        File is=new File(new FileInputStream(filePath));
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while ((line = reader.readLine()) != null) { // 如果 line 为空说明读完了
            for (int a = 0; a < conditions.length; a++) {
                if (line.contains(conditions[a])) {// 如果包含这个关键字,直接返回
                    return true;
                } else {// 如果某一个关键字不包含在名字中,继续判断下面的关键字
                    continue;
                }
            }
        }
        reader.close();
        is.close();
        return Boolean.FALSE;
    }
    /**
     * **
     *
     * @param entryName
     * @param conditions
     * @return
     */
    public static boolean isOkToAdd(String entryName, String[] conditions) {
        if (FileListTableImpl.getInstance().isAnd()) {// 如果是and条件
            for (int a = 0; a < conditions.length; a++) {
                if (isFile(entryName)) {// 如果是文件
                    if (isOnlyCheckName()) {// 如果仅仅在jarentry的名字中寻找而不在名字前面的路径中查找
                        if (getEntryFileName(entryName).contains(conditions[a])) {// 如果包含这个关键字,继续判断下一个关键字
                            continue;
                        } else {// 如果某一个关键字不包含在名字中,返回false, 不符合and
                            return false;
                        }
                    } else {// 如果在整个jarentry的全路径中寻找
                        if (entryName.contains(conditions[a])) {// 如果包含这个关键字,继续判断下一个关键字
                            continue;
                        } else {// 如果某一个关键字不包含在名字中,返回false, 不符合and
                            return false;
                        }
                    }
                } else {// 如果是目录
                    return false;
                }
            }
            return true;
        } else {// 如果是 or 条件
            for (int a = 0; a < conditions.length; a++) {
                if (isOnlyCheckName()) {// 如果仅仅在名字中寻找
                    if (isFile(entryName)) {// 如果是文件
                        if (getEntryFileName(entryName).contains(conditions[a])) {// 如果包含这个关键字,返回
                            return true;
                        }
                        continue;
                    } else {// 如果是目录
                        return false;
                    }
                } else {// 如果在整个路径中寻找
                    if (entryName.contains(conditions[a])) {// 如果路径中包含这个关键字
                        if (isFile(entryName)) {// 如果是文件
                            return true;
                        } else {// 如果是目录
                            return false;
                        }
                    } else {//
                        continue;
                    }
                }
            }
            return false;//
        }
    }
    private static boolean isOnlyCheckName() {
        return FileListTableImpl.getInstance().isOnlyCheckName();
    }
    private static boolean isCheckFileContent() {
        return FileListTableImpl.getInstance().isCheckFileContent();
    }
    private static String getEntryFileName(String entryname) {
        if (entryname == null || entryname.trim().length() <= 0) {
            return null;
        }
        int index = entryname.lastIndexOf('/');
        String filename = entryname.substring(index + 1, entryname.length());
        return filename;
    }
    /**
     * 判断这个 entry 是否是一个文件
     *
     * @param jarentryname
     * @return
     */
    private static boolean isFile(String jarentryname) {
        if (jarentryname == null || jarentryname.trim().length() <= 0) {
            return false;
        }
        int index = jarentryname.lastIndexOf('/');
        int len = jarentryname.trim().length();
        if (index == len - 1) {// directory
            return false;
        }
        if (index == -1) {// 单层目录文件
            return true;
        }
        if (len > index) {// 多层目录文件
            return true;
        }
        return true;
    }
}
上一篇:ip归属地查询
下一篇:短8为uuid
网友评论