Monday, 22 March 2021

Directory tree in Java

  • Start your IDE and paste the following code.
  • Execute the code to build a directory tree of your selected folder. 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintStream;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.Scanner;

 

public class file {

       static void print(File dir, int tab) throws IOException {

              File lastfolder = null;

              try {

                     for (int j = 0; j < tab - 1; j++) {

                           System.out.print("│   ");

                     }

                     for (int i = 0; i < dir.getParentFile().listFiles().length; i++) {

                           lastfolder = dir.getParentFile().listFiles()[i];

                     }

                     if (lastfolder.equals(dir) && !(tab == 0)) {

                           System.out.print("└──");

                     } else if (!(tab == 0)) {

                           System.out.print("├──");

                     }

                     printdir(dir);

              } catch (NullPointerException e) {

                     printdir(dir);

              }

              try {

                     for (int i = 0; i < dir.listFiles().length - 1; i++) {

                           if (dir.listFiles()[i].isFile() && dir.listFiles()[i].exists()) {

                                  for (int j = 0; j < tab; j++) {

                                         System.out.print("│   ");

                                  }

                                  /*if (!dir.equals(lastfolder)) {

                                         System.out.print("│   ");

                                  }

                                  else {

                                         System.out.print("    ");

                                  }*/

                                  System.out.print("├──");

                                  printfile(dir.listFiles()[i]);

                                  System.out.println();

                           } else {

                                  print(dir.listFiles()[i], tab + 1);

                           }

                     }

                     int l = dir.listFiles().length - 1;

                     if (l >= 0) {

                           if (dir.listFiles()[l].isFile() && dir.listFiles()[l].exists()) {

                                  for (int j = 0; j < tab; j++) {

                                         System.out.print("│   ");

                                  }

                                  System.out.print("└──");

                                  printfile(dir.listFiles()[l]);

                                  System.out.println();

                           } else {

                                  print(dir.listFiles()[l], tab + 1);

                           }

                     }

              } catch (NullPointerException e) {

                     // TODO: handle exception

              }

       }

 

       private static void printdir(File dir) throws IOException {

              BasicFileAttributes attr=Files.readAttributes(Paths.get(dir.toString()), BasicFileAttributes.class);

              System.out.print("[" + dir + "]"+" /CreatedOn: "+attr.creationTime()+" LastModified: "+attr.lastModifiedTime()+" LastAccesedOn: "+attr.lastAccessTime()+" Size: "+(double)(Files.size(Paths.get(dir.toString())))/1024+"kB/");

              System.out.println();

 

       }

 

       private static void printfile(File file) throws IOException {

              BasicFileAttributes attr=Files.readAttributes(Paths.get(file.toString()), BasicFileAttributes.class);

              System.out.print("("+file +") /CreatedOn: "+attr.creationTime()+" LastModifiedOn: "+attr.lastModifiedTime()+" LastAccesedOn: "+attr.lastAccessTime()+" Size: "+(double)(Files.size(Paths.get(file.toString())))/1024+"kB/");

 

       }

       @SuppressWarnings("resource")

       public static void main(String[] args) throws IOException {

              System.out.print("Enter search directory name: ");

              File dir = new File(new Scanner(System.in).next());

              System.out.print("Enter output filename: ");

              try {

                     System.setOut(new PrintStream(new File(new Scanner(System.in).next())));

              } catch (FileNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

              if (dir.isDirectory() && dir.exists()) {

                     print(dir, 0);

              }

 

       }

 

}

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.