Search in sources :

Example 61 with IORuntimeException

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

the class FileWriter method write.

// ------------------------------------------------------- Constructor end
/**
 * 将String写入文件
 *
 * @param content  写入的内容
 * @param isAppend 是否追加
 * @return 目标文件
 * @throws IORuntimeException IO异常
 */
public File write(String content, boolean isAppend) throws IORuntimeException {
    BufferedWriter writer = null;
    try {
        writer = getWriter(isAppend);
        writer.write(content);
        writer.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(writer);
    }
    return file;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 62 with IORuntimeException

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

the class FileWriter method writeFromStream.

/**
 * 将流的内容写入文件
 *
 * @param in        输入流,不关闭
 * @param isCloseIn 是否关闭输入流
 * @return dest
 * @throws IORuntimeException IO异常
 * @since 5.5.2
 */
public File writeFromStream(InputStream in, boolean isCloseIn) 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);
        if (isCloseIn) {
            IoUtil.close(in);
        }
    }
    return file;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 63 with IORuntimeException

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

the class Tailer method start.

/**
 * 开始监听
 *
 * @param async 是否异步执行
 */
public void start(boolean async) {
    // 初始读取
    try {
        this.readTail();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    final LineReadWatcher lineReadWatcher = new LineReadWatcher(this.randomAccessFile, this.charset, this.lineHandler);
    final ScheduledFuture<?> scheduledFuture = // 
    this.executorService.scheduleAtFixedRate(// 
    lineReadWatcher, // 
    0, // 
    this.period, // 
    TimeUnit.MILLISECONDS);
    if (false == async) {
        try {
            scheduledFuture.get();
        } catch (ExecutionException e) {
            throw new UtilException(e);
        } catch (InterruptedException e) {
        // ignore and exist
        }
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 64 with IORuntimeException

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

the class Tailer method readTail.

// ---------------------------------------------------------------------------------------- Private method start
/**
 * 预读取行
 *
 * @throws IOException IO异常
 */
private void readTail() throws IOException {
    final long len = this.randomAccessFile.length();
    if (initReadLine > 0) {
        Stack<String> stack = new Stack<>();
        long start = this.randomAccessFile.getFilePointer();
        long nextEnd = len - 1;
        this.randomAccessFile.seek(nextEnd);
        int c;
        int currentLine = 0;
        while (nextEnd > start) {
            // 满
            if (currentLine > initReadLine) {
                break;
            }
            c = this.randomAccessFile.read();
            if (c == CharUtil.LF || c == CharUtil.CR) {
                // FileUtil.readLine(this.randomAccessFile, this.charset, this.lineHandler);
                final String line = FileUtil.readLine(this.randomAccessFile, this.charset);
                if (null != line) {
                    stack.push(line);
                }
                currentLine++;
                nextEnd--;
            }
            nextEnd--;
            this.randomAccessFile.seek(nextEnd);
            if (nextEnd == 0) {
                // 当文件指针退至文件开始处,输出第一行
                // FileUtil.readLine(this.randomAccessFile, this.charset, this.lineHandler);
                final String line = FileUtil.readLine(this.randomAccessFile, this.charset);
                if (null != line) {
                    stack.push(line);
                }
                break;
            }
        }
        // 输出缓存栈中的内容
        while (false == stack.isEmpty()) {
            this.lineHandler.handle(stack.pop());
        }
    }
    // 将指针置于末尾
    try {
        this.randomAccessFile.seek(len);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) Stack(java.util.Stack)

Example 65 with IORuntimeException

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

the class ChannelCopier method copy.

@Override
public long copy(ReadableByteChannel source, WritableByteChannel 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, ByteBuffer.allocate(bufferSize(this.count)), progress);
    } 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)

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