apache-commons-io

Apache Commons IO Tailer example

半城伤御伤魂 提交于 2019-11-29 03:38:29
I am working on a monitoring program that reads the /var/log/auth.log file. I am using Apache Commons IO Tailer class to read the file in real time. To get started, I wanted to test the real-time reading part on a simple file, and manually enter some code in the console line. Here is my code: 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

Download file using java apache commons?

人走茶凉 提交于 2019-11-28 20:45:30
How can I use the library to download a file and print out bytes saved? I tried using 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); } } but I cannot display bytes or a progress bar. Which method should I use? public class download { public static void Download() { URL dl =

Is it safe to use Apache commons-io IOUtils.closeQuietly?

耗尽温柔 提交于 2019-11-28 19:46:21
问题 Is this code BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt")); try { bw.write("test"); } finally { IOUtils.closeQuietly(bw); } safe or not? As far as I understand when we close a BufferedWriter it will flush its buffer to the underlying stream and may fail due to an error. But IOUtils.closeQuietly API says that any exceptions will be ignored. Is it possible a data loss will go unnoticed due to IOUtils.closeQuietly? 回答1: The code should look like this regarding to the javadoc

Apache Commons IO Tailer example

痴心易碎 提交于 2019-11-27 17:37:51
问题 I am working on a monitoring program that reads the /var/log/auth.log file. I am using Apache Commons IO Tailer class to read the file in real time. To get started, I wanted to test the real-time reading part on a simple file, and manually enter some code in the console line. Here is my code: 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

Download file using java apache commons?

情到浓时终转凉″ 提交于 2019-11-27 13:21:58
问题 How can I use the library to download a file and print out bytes saved? I tried using 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); } } but I cannot display bytes or a

Reading a specific line from a text file in Java

寵の児 提交于 2019-11-26 06:46:56
问题 Is there any method to read a specific line from a text file ? In the API or Apache Commons. Something like : String readLine(File file, int lineNumber) I agree it\'s trivial to implement, but it\'s not very efficient specially if the file is very big. 回答1: String line = FileUtils.readLines(file).get(lineNumber); would do, but it still has the efficiency problem. Alternatively, you can use: LineIterator it = IOUtils.lineIterator( new BufferedReader(new FileReader("file.txt"))); for (int