Search in sources :

Example 1 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class FileCopier method internalCopyDirContent.

// ----------------------------------------------------------------------------------------- Private method start
/**
 * 拷贝目录内容,只用于内部,不做任何安全检查<br>
 * 拷贝内容的意思为源目录下的所有文件和目录拷贝到另一个目录下,而不拷贝源目录本身
 *
 * @param src 源目录
 * @param dest 目标目录
 * @throws IORuntimeException IO异常
 */
private void internalCopyDirContent(File src, File dest) throws IORuntimeException {
    if (null != copyFilter && false == copyFilter.accept(src)) {
        // 被过滤的目录跳过
        return;
    }
    if (false == dest.exists()) {
        // 目标为不存在路径,创建为目录
        dest.mkdirs();
    } else if (false == dest.isDirectory()) {
        throw new IORuntimeException(StrUtil.format("Src [{}] is a directory but dest [{}] is a file!", src.getPath(), dest.getPath()));
    }
    final String[] files = src.list();
    File srcFile;
    File destFile;
    for (String file : files) {
        srcFile = new File(src, file);
        destFile = new File(dest, file);
        // 递归复制
        if (srcFile.isDirectory()) {
            internalCopyDirContent(srcFile, destFile);
        } else {
            internalCopyFile(srcFile, destFile);
        }
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) File(java.io.File)

Example 2 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class FileWriter method writeFromStream.

/**
 * 将流的内容写入文件<br>
 * 此方法不会关闭输入流
 *
 * @param in 输入流,不关闭
 * @return dest
 * @throws IORuntimeException IO异常
 */
public File writeFromStream(InputStream in) throws IORuntimeException {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(FileUtil.touch(file));
        IoUtil.copy(in, out);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(out);
    }
    return file;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 3 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class FileWriter method write.

/**
 * 写入数据到文件
 *
 * @param data 数据
 * @param off 数据开始位置
 * @param len 数据长度
 * @param isAppend 是否追加模式
 * @return 目标文件
 * @throws IORuntimeException IO异常
 */
public File write(byte[] data, int off, int len, boolean isAppend) throws IORuntimeException {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(FileUtil.touch(file), isAppend);
        out.write(data, off, len);
        out.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(out);
    }
    return file;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 4 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class WatchMonitor method init.

// ------------------------------------------------------ Constructor method end
/**
 * 初始化<br>
 * 初始化包括:
 * <pre>
 * 1、解析传入的路径,判断其为目录还是文件
 * 2、创建{@link WatchService} 对象
 * </pre>
 *
 * @throws WatchException 监听异常,IO异常时抛出此异常
 */
public void init() throws WatchException {
    // 获取目录或文件路径
    if (false == Files.exists(this.path, LinkOption.NOFOLLOW_LINKS)) {
        final Path lastPathEle = FileUtil.getLastPathEle(this.path);
        if (null != lastPathEle) {
            final String lastPathEleStr = lastPathEle.toString();
            // 带有点表示有扩展名,按照未创建的文件对待。Linux下.d的为目录,排除之
            if (StrUtil.contains(lastPathEleStr, StrUtil.C_DOT) && false == StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) {
                this.filePath = this.path;
                this.path = this.filePath.getParent();
            }
        }
        // 创建不存在的目录或父目录
        try {
            Files.createDirectories(this.path);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    } else if (Files.isRegularFile(this.path, LinkOption.NOFOLLOW_LINKS)) {
        this.filePath = this.path;
        this.path = this.filePath.getParent();
    }
    // 初始化监听
    try {
        watchService = FileSystems.getDefault().newWatchService();
    } catch (IOException e) {
        throw new WatchException(e);
    }
    isClosed = false;
}
Also used : Path(java.nio.file.Path) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 5 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class CsvParser method readLine.

/**
 * 读取一行数据
 *
 * @return 一行数据
 * @throws IORuntimeException IO异常
 */
private List<String> readLine() throws IORuntimeException {
    final List<String> currentFields = new ArrayList<>(maxFieldCount > 0 ? maxFieldCount : DEFAULT_ROW_CAPACITY);
    final StrBuilder localCurrentField = currentField;
    final char[] localBuf = this.buf;
    // 当前位置
    int localBufPos = bufPos;
    // 前一个特殊分界字符
    int localPreChar = preChar;
    // 拷贝起始位置
    int localCopyStart = copyStart;
    // 拷贝长度
    int copyLen = 0;
    while (true) {
        if (bufLen == localBufPos) {
            if (copyLen > 0) {
                localCurrentField.append(localBuf, localCopyStart, copyLen);
            }
            try {
                bufLen = reader.read(localBuf);
            } catch (IOException e) {
                throw new IORuntimeException(e);
            }
            if (bufLen < 0) {
                // CSV读取结束
                finished = true;
                if (localPreChar == config.fieldSeparator || localCurrentField.hasContent()) {
                    // 剩余部分作为一个字段
                    currentFields.add(localCurrentField.toStringAndReset());
                }
                break;
            }
            // 重置
            localCopyStart = localBufPos = copyLen = 0;
        }
        final char c = localBuf[localBufPos++];
        if (inQuotes) {
            // 引号内,做为内容,直到引号结束
            if (c == config.textDelimiter) {
                // End of quoted text
                inQuotes = false;
            } else {
                if ((c == CharUtil.CR || c == CharUtil.LF) && localPreChar != CharUtil.CR) {
                    lineNo++;
                }
            }
            copyLen++;
        } else {
            if (c == config.fieldSeparator) {
                // 一个字段结束
                if (copyLen > 0) {
                    localCurrentField.append(localBuf, localCopyStart, copyLen);
                    copyLen = 0;
                }
                currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
                localCopyStart = localBufPos;
            } else if (c == config.textDelimiter) {
                // 引号开始
                inQuotes = true;
                copyLen++;
            } else if (c == CharUtil.CR) {
                if (copyLen > 0) {
                    localCurrentField.append(localBuf, localCopyStart, copyLen);
                }
                currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
                localPreChar = c;
                localCopyStart = localBufPos;
                break;
            } else if (c == CharUtil.LF) {
                if (localPreChar != CharUtil.CR) {
                    if (copyLen > 0) {
                        localCurrentField.append(localBuf, localCopyStart, copyLen);
                    }
                    currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
                    localPreChar = c;
                    localCopyStart = localBufPos;
                    break;
                }
                localCopyStart = localBufPos;
            } else {
                copyLen++;
            }
        }
        localPreChar = c;
    }
    // restore fields
    bufPos = localBufPos;
    preChar = localPreChar;
    copyStart = localCopyStart;
    return currentFields;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StrBuilder(cn.hutool.core.text.StrBuilder)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)25 IOException (java.io.IOException)19 File (java.io.File)4 FastByteArrayOutputStream (cn.hutool.core.io.FastByteArrayOutputStream)2 Rectangle (java.awt.Rectangle)2 BufferedImage (java.awt.image.BufferedImage)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 UtilException (cn.hutool.core.exceptions.UtilException)1 StrBuilder (cn.hutool.core.text.StrBuilder)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 JSchException (com.jcraft.jsch.JSchException)1 EOFException (java.io.EOFException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 Socket (java.net.Socket)1