Archives: Apache Commons

Java – Change File Date / Time using Commons FileUtils

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]


Java – URL Validation using Commons UrlValidator

[code lang=”java”]import org.apache.commons.validator.UrlValidator;

public class ValidateUrlExample {

public static void main(String[] args) {

UrlValidator urlValidator = new UrlValidator();

//valid URL
if (urlValidator.isValid("http://www.javac.in")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}

//invalid URL
if (urlValidator.isValid("http://invalidURL^$&%$&^")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}

}
}[/code]