Archives: Commons FileUtils

Java – Get List of Files from FTP Server


Java – Change File Date / Time using Commons FileUtils

[code lang=”java”]import java.io.IOException;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class Main {

public static void main(String[] args) {
try {
File file = new File("pic.jpg");
long lastModified1 = file.lastModified();
FileUtils.touch(file);
long lastModified2 = file.lastModified();
System.out.println("File date / time was updated: " + (lastModified2 > lastModified1));
} catch (IOException ex) {
ex.printStackTrace();
}
}

}[/code]

Output:

File date / time was updated: true


Java – Calculate Directory Size using Commons FileUtils

[code lang=”java”]import java.io.File;
import org.apache.commons.io.FileUtils;

public class DirectorySizeSample {

public static void main(String[] args) {

// Using FileUtils.sizeOfDirectory() we can calculate
// the size of a directory including it sub directory
long size = FileUtils.sizeOfDirectory(new File("C:/Windows"));

System.out.println("Size: " + size + " bytes");

}
}[/code]