Saturday, 31 August 2019

[Java] Print all the files under a given directory

Print all the files under a given directory. If there are subdirectories traverse them recursively and figure out all the files inside it.

package test.test;

import java.io.File;

public class Test {
   public static void main(String[] args) {
      filePrintUtil("/Users/poddepally/Downloads");
   }

   private static void filePrintUtil(String pathName) {
      File file = new File(pathName);
      File[] listFiles = file.listFiles();
      for (File currentFile : listFiles) {
          if (currentFile.isFile()) {
              System.out.println(currentFile.getName()
                  + ", length(bytes) =" + currentFile.length()
                  + ", parent = " + currentFile.getParentFile().getName());
   } else {
       filePrintUtil(currentFile.getAbsolutePath());
   }
      }
   }
}

Friday, 16 August 2019

Class Loaders in Java

Class loaders in the JVM is responsible for looking out for the requested class and load it on demand. Unless set otherwise, class loaders look at the default class path locations of java installation such as {JRE_HOME}/lib/rt.jar and {JRE_HOME/ext}. rt.jar file holds the java core class files such as String, integer, etc.
You can set the class path of the application by -cp flag while compilation/execution.
Java contains following type of class loaders by default:
  • Bootstrap class loader: Looks for class files such as java.lang.* in rt.jar
  • Extension class loader: Looks for class file in /ext location.
  • Application class loader: Looks for class file in the location set by the application.

Class loaders follows delegation model while looking for class file. That is, class loader will delegate the load process to parent class loader, if not found, takes the responsibility to load the requested class.

Saturday, 3 August 2019

HTTP Status Codes

HTTP status codes are categorised into 5 buckets. These are very helpful during API development such as REST APIs.
Status Code Meaning
1xx informational
2xx Success
3xx Redirection
4xx Client error
5xx Server error