Max Yuzhakov
Max Yuzhakov

Reputation: 21

Count bytes written to file by FileUtils.copyURLToFile?

How can I count the number of bytes written by Appache Commons FileUtils.copyURLToFile?

Upvotes: 1

Views: 309

Answers (1)

Yevgen
Yevgen

Reputation: 1667

Since copying file from url takes time and you already have Apache Commons on your classpath, you might want to use ConcurrentUtils class for your problem.

long lengthInBytes = ConcurrentUtils.constantFuture(downloadFile()).get();

where downloadFile() method provides an implementation, which returns length of file in bytes

public Long downloadFile() throws IOException {
  File target = new File(fileName);
  FileUtils.copyURLToFile(new URL(url), target);
  return target.length();
}

Upvotes: 1

Related Questions