mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add filter read line
This commit is contained in:
parent
292ceb594b
commit
3c72340868
@ -54,6 +54,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.CRC32;
|
||||
@ -2368,6 +2369,19 @@ public class FileUtil extends PathUtil {
|
||||
return readLines(file, CharsetUtil.CHARSET_UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件中读取每一行数据
|
||||
*
|
||||
* @param file 文件
|
||||
* @param filter 过滤器
|
||||
* @return 文件中的每行内容的集合List
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static List<String> readUtf8Lines(File file, Predicate<String> filter) throws IORuntimeException {
|
||||
return readLines(file, CharsetUtil.CHARSET_UTF_8, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件中读取每一行数据
|
||||
*
|
||||
@ -2392,6 +2406,25 @@ public class FileUtil extends PathUtil {
|
||||
return readLines(file, charset, new ArrayList<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件中读取每一行数据
|
||||
*
|
||||
* @param file 文件
|
||||
* @param charset 字符集
|
||||
* @param filter 过滤器
|
||||
* @return 文件中的每行内容的集合List
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static List<String> readLines(File file, Charset charset, Predicate<String> filter) throws IORuntimeException {
|
||||
final List<String> result = new ArrayList<>();
|
||||
readLines(file, charset, (LineHandler) line -> {
|
||||
if (Boolean.TRUE.equals(filter.test(line))) {
|
||||
result.add(line);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按行处理文件内容,编码为UTF-8
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user