Search in sources :

Example 66 with IORuntimeException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 67 with IORuntimeException

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);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 68 with IORuntimeException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IORuntimeException(cn.hutool.core.io.IORuntimeException)

Example 69 with IORuntimeException

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);
    }
}
Also used : Path(java.nio.file.Path) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 70 with IORuntimeException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) CopyVisitor(cn.hutool.core.io.file.visitor.CopyVisitor) IOException(java.io.IOException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)81 IOException (java.io.IOException)75 File (java.io.File)9 StreamProgress (cn.hutool.core.io.StreamProgress)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 UtilException (cn.hutool.core.exceptions.UtilException)3 POIException (cn.hutool.poi.exceptions.POIException)3 Rectangle (java.awt.Rectangle)3 BufferedImage (java.awt.image.BufferedImage)3 BufferedReader (java.io.BufferedReader)3 SocketChannel (java.nio.channels.SocketChannel)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 StreamGobbler (ch.ethz.ssh2.StreamGobbler)2 CryptoException (cn.hutool.crypto.CryptoException)2 RenderedImage (java.awt.image.RenderedImage)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2