Example Java Program to Search Files in a Folder


Below Java Program lists the file names and directory names available in given folder. To do this implementation , we can get the files list in a folder using File class available in Java API. Iterate the files one by one and write the file name on to console.

If it is identified as a directory instead of a file, then iterate the process as mentioned in directoryCrawler() method in the below class.

package in.javatutorials;

import java.io.File;

/**
* @author malliktalksjava
*
* Search for the files in a folder and prints all file details.
*
*/
public class FolderCrawler {

private static String directoryPath = “D://test”;

/**
* Creating constructor
*/
public FolderCrawler() {
super();
}

/**
* main method
*
* @param ags
*/
public static void main(String[] args) {
FolderCrawler crawler = new FolderCrawler();

File directory = new File(directoryPath);

if (directory == null || !directory.exists()) {
System.out.println(“Directory doesn’t exists!!!”);
return;
}

crawler.directoryCrawler(directory);
}

/**
* Gets all the file and directories and prints accordingly
*
* @param directory
* Directory path where it should search
*/
public void directoryCrawler(File directory) {

// Get List of files in folder and print
File[] filesAndDirs = directory.listFiles();

// Print the root directory name
System.out.println(“-” + directory.getName());

// Iterate the list of files, if it is identified as not a file call
// directoryCrawler method to list all the files in that directory.
for (File file : filesAndDirs) {

if (file.isFile()) {
System.out.println(” |-” + file.getName());
} else {
directoryCrawler(file);

}
}// end of for

}// End of directory Crawler
}

 

Other Useful Links:

Javac/Java searching algorithm for other classes

Example program to reverse a Number in Java

How to find count of duplicates in a List

Threads Interview questions in Java

Merge Sort Example in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sorts the given array using merge sort algorithm
*/
public class MergeSortExample {

private int[] array;
private int[] tempMergArr;
private int length;

/**
* @param args
*
* main method.
* Pass the array to sort method and prints the output array into console.
*/
public static void main(String args[]){

int[] inputArr = {12,25,36,95,86,21,14,52,85,32};
MergeSortExample mergeSorter = new MergeSortExample();
mergeSorter.sort(inputArr);
for(int sortedValue:inputArr){
System.out.print(sortedValue);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is null of not, then pass it to mergeSort Method.
*/
public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
System.out.println(“Given Array is null or does not contain any elements”);
return;
}

this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
mergeSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the array using mergesort algorithm.
*/
private void mergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex – lowerIndex) / 2;
// sort the left side of the array
mergeSort(lowerIndex, middle);
// sort the right side of the array
mergeSort(middle + 1, higherIndex);
// merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

/**
* @param lowerIndex
* @param middle
* @param higherIndex
*
*
*/
private void mergeParts(int lowerIndex, int middle, int higherIndex) {

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}

 

Other Useful Links:

Quick Sort Example Program in Java

Insertion Sort Example program in JAVA

Selection Sort Example Program in Java

Bubble Sort Example in JAVA

Bubble Sort Example in JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

public static void bubbleSort(int arr[]) {
 int count = array.length;
 int var;
 for (int i = count; i >= 0; i--) {
   for (int j = 0; j  array[j+1]) {
          swapNumbers(temp2, var, array);
      }
   }
 printNumbers(array);
 }
}

private static void swapNumbers(int var1, int var2, int[] array) {
   int temp = array[var1];
   array[var1] = array[var2];
   array[var2] = temp;
}

private static void printNumbers(int[] input) {
  for (int i = 0; i < input.length; i++) {
    System.out.println(input[i] + ", ");
  }
}
}

Other Useful Links:

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Number series example program using JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava.in
*
* This program prints the numbers in below pattern
* 1
* 123
* 12345
* 1234567
* 12345
* 123
* 1
*/
class PrintingNumberPatterns{

public static void main(String[] args){
printNumberSeries();
}

/**
* Print the numbers in assending and decending order by iterating it.
*/
private static void printNumberSeries() {
for (int i = 1; i <= 7; i += 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}

for (int i = 5; i >= 1; i -= 2) {
for (int j = 1; j < i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}