use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileReader method read.
/**
* 按照给定的readerHandler读取文件中的数据
*
* @param <T> 读取的结果对象类型
* @param readerHandler Reader处理类
* @return 从文件中read出的数据
* @throws IORuntimeException IO异常
*/
public <T> T read(ReaderHandler<T> readerHandler) throws IORuntimeException {
BufferedReader reader = null;
T result;
try {
reader = FileUtil.getReader(this.file, charset);
result = readerHandler.handle(reader);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(reader);
}
return result;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileReader method readLines.
/**
* 从文件中读取每一行数据
*
* @param <T> 集合类型
* @param collection 集合
* @return 文件中的每行内容的集合
* @throws IORuntimeException IO异常
*/
public <T extends Collection<String>> T readLines(T collection) throws IORuntimeException {
BufferedReader reader = null;
try {
reader = FileUtil.getReader(file, charset);
String line;
while (true) {
line = reader.readLine();
if (line == null) {
break;
}
collection.add(line);
}
return collection;
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(reader);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class FileReader method readBytes.
// ------------------------------------------------------- Constructor end
/**
* 读取文件所有数据<br>
* 文件的长度不能超过 {@link Integer#MAX_VALUE}
*
* @return 字节码
* @throws IORuntimeException IO异常
*/
public byte[] readBytes() throws IORuntimeException {
long len = file.length();
if (len >= Integer.MAX_VALUE) {
throw new IORuntimeException("File is larger then max array size");
}
byte[] bytes = new byte[(int) len];
FileInputStream in = null;
int readLength;
try {
in = new FileInputStream(file);
readLength = in.read(bytes);
if (readLength < len) {
throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", len, readLength));
}
} catch (Exception e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
}
return bytes;
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class PathUtil method copyFile.
/**
* 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件<br>
* 此方法不支持递归拷贝目录,如果src传入是目录,只会在目标目录中创建空目录
*
* @param src 源文件路径,如果为目录只在目标中创建新目录
* @param target 目标文件或目录,如果为目录使用与源文件相同的文件名
* @param options {@link StandardCopyOption}
* @return Path
* @throws IORuntimeException IO异常
* @since 5.4.1
*/
public static Path copyFile(Path src, Path target, CopyOption... options) throws IORuntimeException {
Assert.notNull(src, "Source File is null !");
Assert.notNull(target, "Destination File or directory is null !");
final Path targetPath = isDirectory(target) ? target.resolve(src.getFileName()) : target;
// 创建级联父目录
mkParentDirs(targetPath);
try {
return Files.copy(src, targetPath, options);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
use of cn.hutool.core.io.IORuntimeException in project hutool by looly.
the class PathUtil method copyContent.
/**
* 拷贝目录下的所有文件或目录到目标目录中,此方法不支持文件对文件的拷贝。
* <ul>
* <li>源文件为目录,目标也为目录或不存在,则拷贝目录下所有文件和目录到目标目录下</li>
* <li>源文件为文件,目标为目录或不存在,则拷贝文件到目标目录下</li>
* </ul>
*
* @param src 源文件路径,如果为目录只在目标中创建新目录
* @param target 目标目录,如果为目录使用与源文件相同的文件名
* @param options {@link StandardCopyOption}
* @return Path
* @throws IORuntimeException IO异常
* @since 5.5.1
*/
public static Path copyContent(Path src, Path target, CopyOption... options) throws IORuntimeException {
Assert.notNull(src, "Src path must be not null !");
Assert.notNull(target, "Target path must be not null !");
try {
Files.walkFileTree(src, new CopyVisitor(src, target, options));
} catch (IOException e) {
throw new IORuntimeException(e);
}
return target;
}
Aggregations