Created
April 5, 2011 09:25
-
-
Save Cellane/903315 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FilenameFilter; | |
public class ExtensionFilter implements FilenameFilter { | |
String extension; | |
public ExtensionFilter (String extension) { | |
this.extension = extension; | |
} | |
public boolean accept (File file, String fileName) { | |
fileName = fileName.toLowerCase (); | |
return (fileName.endsWith (extension)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.text.SimpleDateFormat; | |
import java.util.Scanner; | |
public class Main { | |
public static ExtensionFilter extensionFilter; | |
public static int directoriesNum = 0, filesNum = 0; | |
public static long sizeTotal = 0; | |
public static void main (String[] args) { | |
File directory; | |
File[] files; | |
Scanner scanner = new Scanner (System.in); | |
String dirName, filter; | |
scanner.useDelimiter ("\n"); | |
System.out.print ("Nazev adresare: "); | |
dirName = scanner.next (); | |
System.out.print ("Filtr: "); | |
filter = scanner.next (); | |
extensionFilter = new ExtensionFilter (filter); | |
directory = new File (System.getProperty ("user.home") + File.separator + dirName); | |
if (directory.exists ()) { | |
System.out.format ("%-30s%15s %-21s%3s\n", "Nazev souboru", "Velikost", "Vytvoreno", "Prava"); | |
printSeparator (); | |
files = directory.listFiles (); | |
listDirectories (files); | |
listFiles (files); | |
printSeparator (); | |
System.out.println ("Adresaru: " + directoriesNum + ", souboru: " + filesNum + " o celkove velikosti " | |
+ sizeTotal + " bajtu."); | |
} else { | |
System.out.println ("Soubor neexistuje."); | |
} | |
} | |
private static void listFiles (File[] files) { | |
String modified, length, accessRights; | |
for (File file : files) { | |
if (file.isFile () && extensionFilter.accept (file, file.getName ())) { | |
length = file.length () + "b"; | |
modified = getModified (file); | |
accessRights = getAccessRights (file); | |
System.out.format ("%-30s%15s %-21s%-3s\n", file.getName (), length, modified, accessRights); | |
sizeTotal += file.length (); | |
filesNum++; | |
} | |
} | |
} | |
private static void listDirectories (File[] files) { | |
String modified, accessRights; | |
for (File file : files) { | |
if (file.isDirectory () && extensionFilter.accept (file, file.getName ())) { | |
modified = getModified (file); | |
accessRights = getAccessRights (file); | |
System.out.format ("%-30s%-15s %-21s%-3s\n", file.getName (), "---------------", | |
modified, accessRights); | |
directoriesNum++; | |
} | |
} | |
} | |
private static String getModified (File file) { | |
SimpleDateFormat dateFormat = new SimpleDateFormat ("dd. MM. yy, HH.mm:ss"); | |
String modifiedString; | |
long modified; | |
modified = file.lastModified (); | |
modifiedString = dateFormat.format (modified); | |
return (modifiedString); | |
} | |
private static String getAccessRights (File file) { | |
StringBuffer accessRights = new StringBuffer (); | |
accessRights.append ((file.canRead ()) ? "r" : "-"); | |
accessRights.append ((file.canWrite ()) ? "w" : "-"); | |
accessRights.append ((file.canExecute ()) ? "x" : "-"); | |
return (accessRights.toString ()); | |
} | |
private static void printSeparator () { | |
System.out.println ("========================================================================"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment