Questions tagged [apache-commons-io]

Apache Commons IO is a library of utilities to assist with developing IO functionality.

Apache Commons Commons IO is a library of utilities to assist with developing IO functionality.

There are six main areas included:

  • Utility classes - with static methods to perform common tasks
  • Input - useful Input Stream and Reader implementations
  • Output - useful Output Stream and Writer implementations
  • Filters - various implementations of file filters
  • Comparators - various implementations of java.util.Comparator for files
  • File Monitor - a component for monitoring file system events

Official Website: http://commons.apache.org/io/

Useful Links:

Related Tags:

175 questions
229
votes
11 answers

Delete all files in directory (but not directory) - one liner solution

I want to delete all files inside ABC directory. When I tried with FileUtils.deleteDirectory(new File("C:/test/ABC/")); it also deletes folder ABC. Is there a one liner solution where I can delete files inside directory but not directory?
Fahim Parkar
  • 28,922
  • 40
  • 153
  • 260
38
votes
4 answers

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

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…
Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
33
votes
1 answer

Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io?

Commons-IO has an IOUtils.toString(inputStream) method, which can read all content from an input stream: InputStream input = getInputStream(); String content = IOUtils.toString(input); My question is shall I close the input stream manually after…
Freewind
  • 177,284
  • 143
  • 381
  • 649
21
votes
9 answers

Reading a specific line from a text file in Java

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…
Lluis Martinez
  • 1,868
  • 7
  • 27
  • 40
18
votes
6 answers

Recursively finding only directories with FileUtils.listFiles

I want to collect a list of all files under a directory, in particular including subdirectories. I like not doing things myself, so I'm using FileUtils.listFiles from Apache Commons IO. So I have something like: import java.io.File; import…
Dave B
  • 1,001
  • 2
  • 8
  • 5
16
votes
2 answers

Apache Commons IO Tailer example

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…
user2435860
  • 738
  • 3
  • 9
  • 19
13
votes
2 answers

Download file using java apache commons?

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 { …
Kyle
  • 2,830
  • 14
  • 49
  • 73
12
votes
3 answers

Problems deleting a file with Java (apache commons io)

I am calling a C++ Method via JNI which creates two files. A text log file and a pdf file in a given directory. I want to delete these files (if they exist) before executing the JNI method. I am using Apache commons.io (FileUtils.forceDelete(File…
user212926
11
votes
1 answer

Duplicated files copied in APK when including both Joda Time and Common IO libraries in Android project

I have an Android project (Gradle) where I need to include both Joda Time and Commons IO libraries. This is my Gradle file: apply plugin: 'android-library' apply plugin: 'android-test' buildscript { repositories { mavenCentral() } …
Juan Herrero Diaz
  • 781
  • 1
  • 6
  • 14
8
votes
1 answer

FileUtils.copyUrlToFile is not working

I have fairly basic question i'm trying to download a PDF from this URL using java:…
Gideon Oduro
  • 178
  • 1
  • 13
6
votes
2 answers

Should I close the InputStream of org.apache.commons.io.IOUtils

I'm using the answer on How to convert InputStream to virtual File which uses org.apache.commons.io.IOUtils to copy the given InputStream to a FileOutputStream in order to create a File. Should I close the InputStream given?
ilopezluna
  • 4,925
  • 7
  • 37
  • 66
6
votes
1 answer

Efficiently read file from URL into byte[] in Java

I'm trying to find a more efficient method of reading a file from a remote URL and saving it into a byte array. Here is what I currently have: private byte[] fetchRemoteFile(String location) throws Exception { URL url = new URL(location); …
DerStrom8
  • 1,271
  • 2
  • 20
  • 44
6
votes
4 answers

Compress directory into a zipfile with Commons IO

I am a beginner at programming with Java and am currently writing an application which must be able to compress and decompress .zip files. I can use the following code to decompress a zipfile in Java using the built-in Java zip functionality as…
StackUnderflow
  • 411
  • 1
  • 3
  • 14
5
votes
3 answers

Android Studio3.2 APK Build Error -> reserved file or directory name 'lib'

Android Studio Version 3.2 (AI-181.5540.7.32.5014246). In Android Studio 3.1, I was able to build SignedAPK successfully. But as soon as I made Android Studio 3.2, I could not build a SignedAPK at all. What is the cause of this problem? I have not…
user3323951
  • 347
  • 3
  • 10
5
votes
2 answers

Why is ReversedLinesFileReader so slow?

I have a file that is 21.6GB and I want to read it from the end to the start rather than from the beginning to the end as you would usually do. If I read each line of the file from the start to the end using the following code, then it takes 1…
Arthur
  • 1,252
  • 1
  • 19
  • 36
1
2 3
11 12