Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cwiczenie4;
- import java.io.File;
- import java.io.FilenameFilter;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.attribute.BasicFileAttributes;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- public class Main {
- public static void main(String args[]) throws Exception {
- //printFilesSimple("C:\\");
- printFilesDetails("C:\\");
- //printFiles("C:\\", ".txt");
- //printTree("C:\\");
- }
- public static void printFilesSimple(String path) {
- File folder = new File(path);
- File[] listOfFiles = folder.listFiles();
- for (File file : listOfFiles) {
- System.out.println(file.getName());
- }
- }
- public static void printFilesDetails(String path) throws Exception {
- // to be implemented
- Path path2 = Paths.get(path);
- BasicFileAttributes attr;
- attr = Files.readAttributes(path2, BasicFileAttributes.class);
- DateFormat df = new SimpleDateFormat("yyyy/MM/dd mm:ss");
- String dirPath = path;
- File dir = new File(dirPath);
- File[] files = dir.listFiles();
- if (files.length == 0) {
- System.out.println("The directory is empty");
- } else {
- for (File aFile : files) {
- //System.out.println(aFile.getName() + " - " +
- if(aFile.isDirectory()) {
- System.out.println(aFile.getName() + " DIR ");
- } else {
- System.out.println(aFile.getName() + " "+attr.size()+" "+df.format(attr.creationTime().toMillis()));
- }
- }
- }
- }
- public static void printFiles(String path, String extensionFilter) {
- // to be implemented
- FilenameFilter mp3Filter = new FilenameFilter() {
- public boolean accept(File file, String name) {
- if (name.endsWith(extensionFilter)) {
- // match files extension
- return true;
- } else {
- return false;
- }
- }
- };
- String dirPath = path;
- File dir = new File(dirPath);
- File[] files = dir.listFiles(mp3Filter);
- if (files.length == 0) {
- System.out.println("There is no "+extensionFilter+" files");
- } else {
- for (File aFile : files) {
- System.out.println(aFile.getName() + " - " + aFile.length());
- }
- }
- }
- public static void printTree(String path) {
- // to be implemented
- // Example
- // dirname
- // dirname/file1
- // dirname/file2
- // dirname/dirname1
- // dirname/dirname1/file1
- // dirname/dirname1/file2
- // dirname/dirname2/file1
- String dirPath = path;
- String longPath = "";
- File dir = new File(dirPath);
- File[] subDirectories = dir.listFiles();
- if (subDirectories.length == 0) {
- System.out.println("The directory is empty");
- } else {
- for (File aFile : subDirectories) {
- if(aFile.isFile()) {
- System.out.println(aFile.getAbsolutePath());
- }else if (aFile.isDirectory()) {
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment