Search in sources :

Example 26 with IORuntimeException

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

the class LineReadWatcher method onModify.

@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
    final RandomAccessFile randomAccessFile = this.randomAccessFile;
    final Charset charset = this.charset;
    final LineHandler lineHandler = this.lineHandler;
    try {
        final long currentLength = randomAccessFile.length();
        final long position = randomAccessFile.getFilePointer();
        if (position == currentLength) {
            // 内容长度不变时忽略此次事件
            return;
        } else if (currentLength < position) {
            // 如果内容变短或变0,说明文件做了删改或清空,回到内容末尾或0
            randomAccessFile.seek(currentLength);
            return;
        }
        // 读取行
        FileUtil.readLines(randomAccessFile, charset, lineHandler);
        // 记录当前读到的位置
        randomAccessFile.seek(currentLength);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : LineHandler(cn.hutool.core.io.LineHandler) RandomAccessFile(java.io.RandomAccessFile) IORuntimeException(cn.hutool.core.io.IORuntimeException) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 27 with IORuntimeException

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

the class PathUtil method moveContent.

/**
 * 移动文件或目录内容到目标目录中,例如:
 * <ul>
 *     <li>moveContent("/usr/aaa/abc.txt", "/usr/bbb")结果为:"/usr/bbb/abc.txt"</li>
 *     <li>moveContent("/usr/aaa", "/usr/bbb")结果为:"/usr/bbb"</li>
 * </ul>
 *
 * @param src        源文件或目录路径
 * @param target     目标路径,如果为目录,则移动到此目录下
 * @param isOverride 是否覆盖目标文件
 * @return 目标文件Path
 * @since 5.7.9
 */
public static Path moveContent(Path src, Path target, boolean isOverride) {
    Assert.notNull(src, "Src path must be not null !");
    Assert.notNull(target, "Target path must be not null !");
    final CopyOption[] options = isOverride ? new CopyOption[] { StandardCopyOption.REPLACE_EXISTING } : new CopyOption[] {};
    // 自动创建目标的父目录
    mkParentDirs(target);
    try {
        return Files.move(src, target, options);
    } catch (IOException e) {
        if (e instanceof FileAlreadyExistsException) {
            // issue#I4QV0L@Gitee
            throw new IORuntimeException(e);
        }
        // 移动失败,可能是跨分区移动导致的,采用递归移动方式
        try {
            Files.walkFileTree(src, new MoveVisitor(src, target, options));
            // 移动后空目录没有删除,
            del(src);
        } catch (IOException e2) {
            throw new IORuntimeException(e2);
        }
        return target;
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) MoveVisitor(cn.hutool.core.io.file.visitor.MoveVisitor) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 28 with IORuntimeException

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

the class ReaderWriterCopier method copy.

@Override
public long copy(Reader source, Writer target) {
    Assert.notNull(source, "InputStream is null !");
    Assert.notNull(target, "OutputStream is null !");
    final StreamProgress progress = this.progress;
    if (null != progress) {
        progress.start();
    }
    final long size;
    try {
        size = doCopy(source, target, new char[bufferSize(this.count)], progress);
        target.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    if (null != progress) {
        progress.finish();
    }
    return size;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) StreamProgress(cn.hutool.core.io.StreamProgress)

Example 29 with IORuntimeException

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

the class Ftp method lsFiles.

/**
 * 遍历某个目录下所有文件和目录,不会递归遍历
 *
 * @param path 目录,如果目录不存在,抛出异常
 * @return 文件或目录列表
 * @throws FtpException       路径不存在
 * @throws IORuntimeException IO异常
 */
public FTPFile[] lsFiles(String path) throws FtpException, IORuntimeException {
    String pwd = null;
    if (StrUtil.isNotBlank(path)) {
        pwd = pwd();
        if (false == isDir(path)) {
            throw new FtpException("Change dir to [{}] error, maybe path not exist!", path);
        }
    }
    FTPFile[] ftpFiles;
    try {
        ftpFiles = this.client.listFiles();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        // 回到原目录
        cd(pwd);
    }
    return ftpFiles;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 30 with IORuntimeException

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

the class Ftp method init.

/**
 * 初始化连接
 *
 * @param config FTP配置
 * @param mode   模式
 * @return this
 */
public Ftp init(FtpConfig config, FtpMode mode) {
    final FTPClient client = new FTPClient();
    // issue#I3O81Y@Gitee
    client.setRemoteVerificationEnabled(false);
    final Charset charset = config.getCharset();
    if (null != charset) {
        client.setControlEncoding(charset.toString());
    }
    client.setConnectTimeout((int) config.getConnectionTimeout());
    final String systemKey = config.getSystemKey();
    if (StrUtil.isNotBlank(systemKey)) {
        final FTPClientConfig conf = new FTPClientConfig(systemKey);
        final String serverLanguageCode = config.getServerLanguageCode();
        if (StrUtil.isNotBlank(serverLanguageCode)) {
            conf.setServerLanguageCode(config.getServerLanguageCode());
        }
        client.configure(conf);
    }
    // connect
    try {
        // 连接ftp服务器
        client.connect(config.getHost(), config.getPort());
        client.setSoTimeout((int) config.getSoTimeout());
        // 登录ftp服务器
        client.login(config.getUser(), config.getPassword());
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    // 是否成功登录服务器
    final int replyCode = client.getReplyCode();
    if (false == FTPReply.isPositiveCompletion(replyCode)) {
        try {
            client.disconnect();
        } catch (IOException e) {
        // ignore
        }
        throw new FtpException("Login failed for user [{}], reply code is: [{}]", config.getUser(), replyCode);
    }
    this.client = client;
    if (mode != null) {
        setMode(mode);
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FTPClientConfig(org.apache.commons.net.ftp.FTPClientConfig) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

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