Friday 6 December 2013

Java Code To Search Directories Recursively For A File

Here is the Java Code to search for a file recursively through the code.
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileSearch {
     
     static List<String> result = new ArrayList<String>();
     
     public static void main(String[] args) {
          String dirc = "C:\\Users\\naved.alam\\workspace\\Test";
          String fName = "FileSearch.java";
          FileSearch.search(new File(dirc),fName);
          int size = result.size();
          if(size ==0){
               System.out.println("\nNo File found.");
          }else{
               System.out.println("\nFound " + size + " File:");
               for (String matched : result){
                    System.out.println("File : " + matched);
               }
          }
     }

     private static void search(File directory,String fileName) {
          if (directory.isDirectory()) {
               System.out.println("Searching directory..." + directory.getAbsoluteFile());
               //Check for permission
               if (directory.canRead()) {
                    for (File temp : directory.listFiles()) {
                         if (temp.isDirectory()) 
                              search(temp,fileName);
                         else if (fileName.equals(temp.getName()))      
                                   result.add(temp.getAbsoluteFile().toString());
                    }

               } 
               else 
                    System.out.println(directory.getAbsoluteFile() + ": Permission Denied");
          }
     }
}
A sample run is shown below:
Searching directory...C:\Users\naved.alam\workspace\Test
Searching directory...C:\Users\naved.alam\workspace\Test\.settings
Searching directory...C:\Users\naved.alam\workspace\Test\backup
Searching directory...C:\Users\naved.alam\workspace\Test\bin
Searching directory...C:\Users\naved.alam\workspace\Test\bin\com
Searching directory...C:\Users\naved.alam\workspace\Test\bin\com\example
Searching directory...C:\Users\naved.alam\workspace\Test\src
Searching directory...C:\Users\naved.alam\workspace\Test\src\com
Searching directory...C:\Users\naved.alam\workspace\Test\src\com\example

Found 2 File:
File : C:\Users\naved.alam\workspace\Test\backup\FileSearch.java
File : C:\Users\naved.alam\workspace\Test\src\com\example\FileSearch.java
Above code can be modified to search for a file matching certain pattern or expression and can be use to integrate advance file searching functionality in an application.

Horizontal Scrolling And Problem Of Word Wrap in Edittext in Android

Recently, I was trying to develop some application for Android device, I came across a bug. I had a edittext with inputType set to 'textMultiLine'.
android:inputType="textMultiLine"

Sunday 1 December 2013

Generating ASCII Art From Input Text Or Image Using JAVA

Wikipedia describe as ASCII art as "A graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII)."

Friday 22 November 2013

C# keywords VS Java keywords

Here is comparison between the keywords available in Java Language and C# Language:

Tuesday 19 November 2013

Calculation of Execution /Elapsed Time in Java

Two Java function can be use to calculate the elapsed time or execuiton time in java.

  1. System.currentTimeMillis()
  2. System.nanoTime()

Example:

System.currentTimeMillis()

 long StartTime = System.currentTimeMillis();
 //do some tasks
 long EndTime = System.currentTimeMillis();
 long difference = EndTime - StartTime;
 
 System.out.println("Elapsed milliseconds: " + difference);

Saturday 16 November 2013

Java Date And Calendar Examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar.

1. Java Date Examples

Few examples to work with Date APIs.

Example 1.1 – Convert Date to String.

 SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
 String date = sdf.format(new Date());
 System.out.println(date); //15/10/2013

Trick To View Private Profile Picture of Any User on Facebook

Facebook has been quick in adapting and fixing bugs. They have been rolling out too many updates in recent past and this has been helping users to find new bugs and tricks. Recently I was able to figure out the way to view profile picture of any user even if it is set to private using privacy settings. The trick isn’t difficult or requires any programming skill. It’s a simple url Hack.

Drawing Circle in Java Using 8-way Symmetry

Beginning with the equation the circle:
We can solve for 'y' in term of 'x':

and use this to compute the pixel of the circle.

Saturday 9 November 2013

Searching an Element in Rotated Sorted Array

One of my friend was ask to devise an algorithm to search a number on a rotated sorted array. Well, we can always do a linear search. But that cannot be optimal we are neglecting the fact that the array was once sorted. The array is still sorted but has been rotated at some point. So, if we could find the rotation point, we could still use binary search to find the number.

Playing with Big O - Complexity of different searching and sorting algorithm

While preparing for interview, I have wasted a lot of my time on summing down the best, average and worst case complexity of different searching and sorting algorithms. To save you guys from going through same pain, I am posting my work here. So, enjoy this cheat sheet while preparing for interview. 

Searching

Algorithm Data Structure Time Complexity Space Complexity
Average Worst Worst
Depth First Search (DFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Breadth First Search (BFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Binary search Sorted array of n elements O(log(n)) O(log(n)) O(1)
Linear (Brute Force) Array O(n) O(n) O(1)

Always include the header files when using some standard C libraries

Yesterday, I was working on a C code of mine when I  came across a very peculiar problem. I even wrote a test code to regenerate the problem I was facing.

int main()
{
    char str[10]="3.5";
    printf("%lf",atof(str));
    return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as