Tag: apache commons io

在使用commons-io的IOUtils.toString(输入)后,是否需要手动关闭输入流?

Commons-IO有一个IOUtils.toString(inputStream)方法,它可以读取输入流中的所有内容: InputStream input = getInputStream(); String content = IOUtils.toString(input); 我的问题是我在使用后手动关闭输入流吗? 我以为IOUtils可能会关闭它,因为它已经阅读了所有内容,但我在源代码中找不到它。

Apache Commons IO Tailer示例

我正在研究一个读取/var/log/auth.log文件的监控程序。 我正在使用Apache Commons IO Tailer类来实时读取文件。 首先,我想在一个简单的文件上测试实时阅读部分,并在控制台行中手动输入一些代码。 这是我的代码: public class Main { public static void main(String[] args) { TailerListener listener = new MyListener(); Tailer tailer = Tailer.create(new File(“log.txt”), listener, 500); while(true) { } } } public class MyListener extends TailerListenerAdapter { @Override public void handle(String line) { System.out.println(line); } } 并且从终端: sudo echo “Hello” >> log.txt问题是当我尝试在文件中手动编写某些东西时,它不会在控制台中打印它。 […]

使用java apache commons下载文件?

如何使用库下载文件并打印保存的字节? 我试过用 import static org.apache.commons.io.FileUtils.copyURLToFile; public static void Download() { URL dl = null; File fl = null; try { fl = new File(System.getProperty(“user.home”).replace(“\\”, “/”) + “/Desktop/Screenshots.zip”); dl = new URL(“http://ds-forums.com/kyle-tests/uploads/Screenshots.zip”); copyURLToFile(dl, fl); } catch (Exception e) { System.out.println(e); } } 但我无法显示字节或进度条。 我应该使用哪种方法? public class download { public static void Download() { URL dl = […]

从Java中的文本文件中读取特定行

有没有方法从文本文件中读取特定的行? 在API或Apache Commons中。 就像是 : String readLine(File file, int lineNumber) 我同意它的实现是微不足道的,但如果文件非常大,它的效率并不高。