mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add test
This commit is contained in:
parent
c8309ef5fb
commit
46d10b4dbc
@ -8,17 +8,13 @@ import cn.hutool.core.lang.func.SerFunction;
|
|||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.*;
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件读取器
|
* 文件读取器
|
||||||
@ -45,7 +41,7 @@ public class FileReader extends FileWrapper {
|
|||||||
* @return FileReader
|
* @return FileReader
|
||||||
*/
|
*/
|
||||||
public static FileReader of(final File file){
|
public static FileReader of(final File file){
|
||||||
return new FileReader(file);
|
return new FileReader(FileUtil.file(file), DEFAULT_CHARSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------- Constructor start
|
// ------------------------------------------------------- Constructor start
|
||||||
@ -58,51 +54,6 @@ public class FileReader extends FileWrapper {
|
|||||||
super(file, charset);
|
super(file, charset);
|
||||||
checkFile();
|
checkFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
* @param file 文件
|
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
|
||||||
*/
|
|
||||||
public FileReader(final File file, final String charset) {
|
|
||||||
this(file, CharsetUtil.charset(charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
|
||||||
* @param charset 编码,使用 {@link CharsetUtil}
|
|
||||||
*/
|
|
||||||
public FileReader(final String filePath, final Charset charset) {
|
|
||||||
this(FileUtil.file(filePath), charset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
|
||||||
* @param charset 编码,使用 {@link CharsetUtil#charset(String)}
|
|
||||||
*/
|
|
||||||
public FileReader(final String filePath, final String charset) {
|
|
||||||
this(FileUtil.file(filePath), CharsetUtil.charset(charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造<br>
|
|
||||||
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
|
||||||
* @param file 文件
|
|
||||||
*/
|
|
||||||
public FileReader(final File file) {
|
|
||||||
this(file, DEFAULT_CHARSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造<br>
|
|
||||||
* 编码使用 {@link FileWrapper#DEFAULT_CHARSET}
|
|
||||||
* @param filePath 文件路径,相对路径会被转换为相对于ClassPath的路径
|
|
||||||
*/
|
|
||||||
public FileReader(final String filePath) {
|
|
||||||
this(filePath, DEFAULT_CHARSET);
|
|
||||||
}
|
|
||||||
// ------------------------------------------------------- Constructor end
|
// ------------------------------------------------------- Constructor end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,16 +64,16 @@ public class FileReader extends FileWrapper {
|
|||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public byte[] readBytes() throws IORuntimeException {
|
public byte[] readBytes() throws IORuntimeException {
|
||||||
final long len = file.length();
|
final long len = this.file.length();
|
||||||
if (len >= Integer.MAX_VALUE) {
|
if (len >= Integer.MAX_VALUE) {
|
||||||
throw new IORuntimeException("File is larger then max array size");
|
throw new IORuntimeException("File is larger then max array size");
|
||||||
}
|
}
|
||||||
|
|
||||||
final byte[] bytes = new byte[(int) len];
|
final byte[] bytes = new byte[(int) len];
|
||||||
FileInputStream in = null;
|
InputStream in = null;
|
||||||
final int readLength;
|
final int readLength;
|
||||||
try {
|
try {
|
||||||
in = new FileInputStream(file);
|
in = FileUtil.getInputStream(this.file);
|
||||||
readLength = in.read(bytes);
|
readLength = in.read(bytes);
|
||||||
if(readLength < len){
|
if(readLength < len){
|
||||||
throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", len, readLength));
|
throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", len, readLength));
|
||||||
@ -155,23 +106,25 @@ public class FileReader extends FileWrapper {
|
|||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public <T extends Collection<String>> T readLines(final T collection) throws IORuntimeException {
|
public <T extends Collection<String>> T readLines(final T collection) throws IORuntimeException {
|
||||||
BufferedReader reader = null;
|
return readLines(collection, null);
|
||||||
try {
|
|
||||||
reader = FileUtil.getReader(file, charset);
|
|
||||||
String line;
|
|
||||||
while (true) {
|
|
||||||
line = reader.readLine();
|
|
||||||
if (line == null) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
collection.add(line);
|
|
||||||
|
/**
|
||||||
|
* 从文件中读取每一行数据
|
||||||
|
*
|
||||||
|
* @param <T> 集合类型
|
||||||
|
* @param collection 集合
|
||||||
|
* @param predicate 断言,断言为真的加入到提供的集合中
|
||||||
|
* @return 文件中的每行内容的集合
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
|
*/
|
||||||
|
public <T extends Collection<String>> T readLines(final T collection, final Predicate<String> predicate) throws IORuntimeException {
|
||||||
|
readLines((SerConsumer<String>) s -> {
|
||||||
|
if(null == predicate || predicate.test(s)){
|
||||||
|
collection.add(s);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
return collection;
|
return collection;
|
||||||
} catch (final IOException e) {
|
|
||||||
throw new IORuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
IoUtil.close(reader);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
package cn.hutool.core.io;
|
package cn.hutool.core.io;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.file.FileUtil;
|
||||||
|
import cn.hutool.core.text.StrUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import cn.hutool.core.io.file.FileReader;
|
import cn.hutool.core.io.file.FileReader;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件读取测试
|
* 文件读取测试
|
||||||
* @author Looly
|
* @author Looly
|
||||||
@ -14,8 +19,22 @@ public class FileReaderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fileReaderTest(){
|
public void fileReaderTest(){
|
||||||
final FileReader fileReader = new FileReader("test.properties");
|
final FileReader fileReader = FileReader.of(FileUtil.file("test.properties"));
|
||||||
final String result = fileReader.readString();
|
final String result = fileReader.readString();
|
||||||
Assert.assertNotNull(result);
|
Assert.assertNotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readLinesTest() {
|
||||||
|
final FileReader fileReader = FileReader.of(FileUtil.file("test.properties"));
|
||||||
|
final List<String> strings = fileReader.readLines();
|
||||||
|
Assert.assertEquals(6, strings.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readLinesTest2() {
|
||||||
|
final FileReader fileReader = FileReader.of(FileUtil.file("test.properties"));
|
||||||
|
final List<String> strings = fileReader.readLines(new ArrayList<>(), StrUtil::isNotBlank);
|
||||||
|
Assert.assertEquals(5, strings.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.lang.func;
|
package cn.hutool.core.lang.func;
|
||||||
|
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import cn.hutool.core.reflect.ConstructorUtil;
|
||||||
import cn.hutool.core.reflect.MethodHandleUtil;
|
import cn.hutool.core.reflect.MethodHandleUtil;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@ -20,6 +21,7 @@ import java.util.Collection;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@ -316,4 +318,15 @@ public class LambdaFactoryTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Test
|
||||||
|
public void buildStringTest() {
|
||||||
|
final char[] a = "1234".toCharArray();
|
||||||
|
|
||||||
|
final Constructor<String> constructor = ConstructorUtil.getConstructor(String.class, char[].class, boolean.class);
|
||||||
|
final BiFunction<char[], Boolean, String> function = LambdaFactory.build(BiFunction.class, constructor);
|
||||||
|
final String apply = function.apply(a, true);
|
||||||
|
Assert.assertEquals(apply, new String(a));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user