Thursday, April 23, 2020

Recursively looping into directories and subdirectories to get files that matching with pattern


  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.regex.*;
  5. public class TechFact555FileMatching {
  6.   // Pattern to match with file name
  7.   public static final String FILE_PATTERN = "[a-zA-Z0-9]{6}";
  8.  
  9.   public static void main(String[] args) {
  10.     List<File> resultFiles = new ArrayList<>();
  11.     TechFact555FileMatching fileMatching = new TechFact555FileMatching();
  12.     
  13.     // Specify parent folder to fetch files
  14.     fileMatching.listFiles(new File("E:/folder"), resultFiles);
  15.    
  16.     for (File resultFile : resultFiles) {
  17.       System.out.print(resultFile.getName());
  18.     }
  19.   }
  20.  
  21.   // Recursively looping into folders to get Files
  22.   void listFiles(File directory, List<File> filesList) {
  23.     File[] filesAndFoldersList = directory.listFiles();
  24.     if (filesAndFoldersList != null) {
  25.       for (File fileOrFolder : filesAndFoldersList) {
  26.         if (fileOrFolder.isDirectory()) {
  27.           listFiles(fileOrFolder, filesList);
  28.         } else if (fileOrFolder.isFile() && Pattern.matches(FILE_PATTERN, fileOrFolder.getName())) {
  29.           filesList.add(fileOrFolder);
  30.         }
  31.       }
  32.     }
  33.   }
  34. }

Pass the Directory to fetch files as first parameter and list of files to add the pattern matching pattern as second parameter into listFiles function.

Java program to handle arithmetic [Calculator] with Exception using Scanner


  1. import java.util.Scanner;
  2. import java.io.*;

  3. public class TechFact555Calculator {
  4.     public static void main(String[] args) {
  5.         Scanner reader = new Scanner(System.in);
  6.         System.out.print("Enter two numbers: ");
  7.         
  8.         // nextDouble() reads the next double from the keyboard
  9.         double first = reader.nextDouble();
  10.         double second = reader.nextDouble();
  11.         System.out.print("Enter an operator (+, -, *, /): ");

  12.         char operator = reader.next().charAt(0);
  13.         double result;
  14.         try {
  15.             switch(operator){

  16.             case '+':
  17.                 result = first + second;
  18.                 break;

  19.             case '-':
  20.                 result = first - second;
  21.                 break;

  22.             case '*':
  23.                 result = first * second;
  24.                 break;

  25.             case '/':
  26.                 result = first / second;
  27.                 break;

  28.             // operator doesn't match any case constant (+, -, *, /)
  29.             default:
  30.                 System.out.printf("Error! operator is not correct");
  31.                 return;
  32.             }
  33.             System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
  34.         } catch(Exception e) {
  35.             System.out.print("Exception occurred " + e);
  36.         }
  37.     }
  38. }

Get token value from request header in java

  1. public class TechFact555 {
  2.  public String getHeader(HttpServletRequest request) {
  3.    String token = request.getHeader("header-key");
  4.    // Validate token string
  5.     return token;
  6.   }
  7. }

To get value in request, use HttpServletRequest object with exact key.

  • NOTE: Header key is case-sensitive.

Program to print numbers from 0 to 50 in JavaScript



  1. var printFiftyNumbers() {
  2. var text = "";
  3. var i;
  4. for(i=0; i <= 50; i++) {
  5. text += i + "<br>";
  6. }
  7. document.getElementById("demo").innerHTML = text;
  8. }

Convert Image to Base64 array in JavaScript

It is very easy to convert image to Base64 String in JavaScript. 

To convert you need jQuery library to your html.

Just pass the image to this below imageFileAsBase64(). Get the Base64 value for the image in imagebase64 variable.


  1. var imagebase64 = "";  
  2.   
  3. function imageFileAsBase64(element) {  
  4.     var file = element.files[0];  
  5.     var reader = new FileReader();  
  6.     reader.onloadend = function() {  
  7.         imagebase64 = reader.result;  
  8.     }  
  9.     reader.readAsDataURL(file);  
  10. }

Complete guide to migrate to Eclipse/Spring Tool Suite (STS) IDE from IntelliJ Idea IDE


Most of us want to use IntelliJ Idea, But due to some reasons like Pricing, Licensing, etc.., We can't able to use IntelliJ Idea. I have sufficient working experience in both Eclipse and IntelliJ. Everyone is using Eclipse as primary IDE because its Free, Open Source, Easy for novice, Capability to work on most of the common frameworks by adding some plugins etc.., Here I have shared how did I changed my Eclipse more like IntelliJ.

Project/Package Explorer:




              Project explorer image of IntelliJ               Project explorer image of Eclipse/STS

I'll prefer to use Project Explorer over Package Explorer in Eclipse/STS. Both are mostly same except some changes, But the following changes can be applied to both Explorer.

Package Presentation:

Eclipse/STS is defaulted the display package as Flat. But IntelliJ uses Hierarchical. 
Three dots in Project/Package Explorer -> Package Presentation -> click Hierarchical.

Hiding Folders and Files:

To Hide some files and some folders in Package/Project Explorer, Click Filter Icon and Check the options whatever you need to hide.
My personal preference: Check these following options
  1. Empty library containers
  2. Empty parent packages
  3. Chromium Virtual File System
  4. Java output folders
  5. Inner class files
  6. Import declarations
  7. Synthetic members

Label/Indicator/Symbol:

In Eclipse, Project/Package Explorer is flooded with lot of Labels in both file and folder icons. To remove those labels:
Window -> Preferences -> Java -> Appearance -> Label Decorations -> Uncheck the options whatever you need to hide. 
My personal preference: Uncheck all options.

 NOTE: Errors and Warnings will continue to display (if any) in Project/Package Explorer despite of this change.

Hide Members of File:

    One thing that annoying most in Eclipse is showing members in files when i tried to open it.
IntelliJ used to show only class files only that file contains more than one class. To remove class extension showing in Project/Package explorer
Window -> Preferences -> Java -> Appearance -> Uncheck "Show members in Package Explorer" checkbox

NOTE: May require restart to work.

Theme:

In IntelliJ, the whole UI is like fixed, boxed. There is no option to do the same in Eclipse. But we can make similar in Eclipse.
Window -> Preferences -> Java -> Appearance -> Theme Dropdown -> Choose Classic or Window Classic

NOTE: May require restarting the Eclipse to apply theme.

Autosaving:

 When you are using IntelliJ, you don't need to save any files. IntelliJ will save all files simultaneously while you are typing. Eclipse added this feature autosaving features in latest edition.
Window -> Editors -> Autosave -> Enable autosave for dirty editors
Set time interval to save files automatically in "Dirty editors autosave interval (in seconds)" field.
My personal preference:  5 seconds.

NOTE: Small value could lead to performance issues.

Auto Popup While Typing:



IntelliJ defaulted to display related details like variables, templates, etc., when you started to typing. In Eclipse, "." ("dot") is defaulted to trigger the content assitant. To change that
Window -> Java -> Editor -> Click Content Assist -> Auto Activation -> Enable auto activation

Change the "Auto activation trigger for Java" as ".abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ". The value is nothing but alphabets with dot and Underscore.

Set time delay for popup in "Auto activation delay (ms)" field.

My personal preference:  40.

NOTE: Small value could lead to performance issues.

Change List/ Quick Diff/ Version Control:


IntelliJ have 'Change List' feature which is quite handy, it will show difference between git current revision to you local change in Editor itself. Eclipse has it But you have to enable it.
Window > Preferences > General > Editors > Text Editors > Quick Diff
            * Check Enable quick diff
            * Check show differences in overview ruler
            * Choose Orange for Changes
            * Choose Green For Additions
            * Choose Pink for Deletions
            * Choose Git revision in Reference source

The Above colors are used in IntelliJ.

Automatic updates:


To stop checking for updated in Eclipse, 
Window > Preferences > General > Install/Update > Automatic updates -> uncheck Automatic update
NOTE: This might increase small performance

Changing Perspective:


Eclipse used to have different perspectives, But IntelliJ don't. Better move the tabs where you have needed in their places and Save it as new Perspective. So that you can reset those tabs in single click.
Window -> Perspective -> Save Perspective as -> IntelliJ [Give a name]
NOTE: It's better to add shortcut for new perspective.

Manual ToolTip:


Eclipse used to show tooltop of everything by just hoving in it. It may reduce performance and IntelliJ doesn't show tooltip on hover. Instead there is an shortcut [Ctrl + Q] to display it.
Window -> Java -> Editor -> Hovers -> Check Javadoc and change it's value to "Shift".
To get ToolTip, just press Shift and hover over it.

NOTE: This might increase performance. 

Fonts and Colors:

Fonts:

IntelliJ uses the "Monospaced" font which is using JVM. I can't find .ttf file for the same. But we can get the similar effect by using "Courier New" and set the size to 7 or 8 in Eclipse.

Coloring:

JavaDoc Coloring:

Text/Content in JavaDoc:

    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

JavaDoc Tag:

    * Add Underscore in effects
    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

JavaDoc HTML Markup:

    * #E2FFE2
    * rgb(226, 255, 226)
    * hsl(120, 100%, 94%)

JavaDoc Others:

    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

Comments Coloring:

   Multiline Comments, Single Line comments:
     * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

Task/TODO tag:

    * Blue color

Java:

Window > Preferences > Java > Editor > Syntax Coloring > "Keyword 'return'" and "Keywords excluding 'return'" 

    ex: return, static

    * #660E7A

    * rgb(102, 14, 122)

    * hsl(289, 79%, 27%)

Annotation color:

    * Bold could be better 
    * #808000
    * rgb(128, 128, 0)
    * hsl(60, 100%, 25%)
Fields:
    * Bold could be better 
    * #660E7A
    * rgb(102, 14, 122)
    * hsl(289, 79%, 27%)     

Static, static finals:

    * Bold could be better 
    * #000080 (Dark blue)
    * rgb(0, 0, 128)
    * hsl(240, 100%, 25%)

Strings:

    * Bold could be better 
    * #008000
    * rgb(0, 128, 0)
    * hsl(120, 100%, 25%)     

Type parameter:

    * #20999D
    * rgb(32, 153, 157)
    * hsl(182, 66%, 37%)

Xml Coloring:

Window > Preferences > Xml > Xml Files > Editor > Syntax Coloring

Attribute names:
    * Bold could be better 
    * #0000FF
    * rgb(0, 0, 255)
    * hsl(240, 100%, 50%)

Attribute Value:

    * Bold could be better 
    * #008000
    * rgb(0, 128, 0)
    * hsl(120, 100%, 25%)

Comment Content, Comment Delimters:

    * Bold could be better 
    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

Entity Reference:

    * Bold could be better 
    * #0000FF (Blue)
    * rgb(0, 0, 255)
    * hsl(240, 100%, 50%)

Namespace Prefix:

    * Bold could be better 
    * #660E7A
    * rgb(102, 14, 122)
    * hsl(289, 79%, 27%)

Tag names:

    * Bold could be better 
    * #000080 (Dark blue)
    * rgb(0, 0, 128)
    * hsl(240, 100%, 25%)

Tag Delimiter & Processing instructions delimiter:

    * Make it Black    

Doc Type/Public System keyword

    * Bold could be better 
    * #0000FF
    * rgb(0, 0, 255)
    * hsl(240, 100%, 50%)

JSON Coloring:

Window > Preferences > JSON > JSON Files > Editor > Syntax coloring

Boolean Value & Null Value:

    * Bold could be better 
    * #000080 (Dark blue)
    * rgb(0, 0, 128)
    * hsl(240, 100%, 25%)
Number value:
    * Bold could be better 
    * #0000FF (Blue)
    * rgb(0, 0, 255)
    * hsl(240, 100%, 50%)

Comment:

    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)
Object key:
    * Make it bold


YEdit Coloring:

Window > Preferences > YEdit Preferences > Color Preferences

Comments:

    * #808080 (Grey)
    * rgb(128, 128, 128)
    * hsl(0, 0%, 50%)

Key:

    * Bold could be better 
    * #000080 (Dark blue)
    * rgb(0, 0, 128)
    * hsl(240, 100%, 25%)

Popup Background:

Window > Preferences > General > Appearance > Color and Fonts

Information background color

    * #F7F7F7 (Light Grey)
    * rgb(247, 247, 247)
    * hsl(0, 0%, 97%)

For shortcuts, I'll publish another post and add link here. Stay tuned.

Don't forgot to extract and backup your eclipse preferences into file.

Happy Coding⌨⌨⌨...

References: stack overflow