Saturday, May 20, 2017

External sort for large file using java


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
public class ExternalSort {
static int N = 2000000; // size of the file in disk
static int M = 100000; // max items the memory buffer can hold
public static void externalSort(String fileName) {
String tfile = "temp-file-";
int[] buffer = new int[M < N ? M : N];
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
int slices = (int) Math.ceil((double) N / M);
int i, j;
i = j = 0;
// Iterate through the elements in the file
for (i = 0; i < slices; i++) {
// Read M-element chunk at a time from the file
for (j = 0; j < (M < N ? M : N); j++) {
String t = br.readLine();
if (t != null)
buffer[j] = Integer.parseInt(t);
else
break;
}
// Sort M elements
Arrays.sort(buffer);
// Write the sorted numbers to temp file
FileWriter fw = new FileWriter(tfile + Integer.toString(i) + ".txt");
PrintWriter pw = new PrintWriter(fw);
for (int k = 0; k < j; k++)
pw.println(buffer[k]);
pw.close();
fw.close();
}
br.close();
fr.close();
// Now open each file and merge them, then write back to disk
int[] topNums = new int[slices];
BufferedReader[] brs = new BufferedReader[slices];
for (i = 0; i < slices; i++) {
brs[i] = new BufferedReader(new FileReader(tfile + Integer.toString(i) + ".txt"));
String t = brs[i].readLine();
if (t != null)
topNums[i] = Integer.parseInt(t);
else
topNums[i] = Integer.MAX_VALUE;
}
FileWriter fw = new FileWriter("C:\\testd\\external-sorted.txt");
PrintWriter pw = new PrintWriter(fw);
for (i = 0; i < N; i++) {
int min = topNums[0];
int minFile = 0;
for (j = 0; j < slices; j++) {
if (min > topNums[j]) {
min = topNums[j];
minFile = j;
}
}
pw.println(min);
String t = brs[minFile].readLine();
if (t != null)
topNums[minFile] = Integer.parseInt(t);
else
topNums[minFile] = Integer.MAX_VALUE;
}
for (i = 0; i < slices; i++)
brs[i].close();
pw.close();
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static String generateInput(int n) {
String fileName = "external-sort.txt";
Random rand = new Random();
try {
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);
for (int i = 0; i < n; i++)
pw.println(rand.nextInt(101));
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public static void main(String[] args) {
String fileName = generateInput(N);
externalSort(fileName);
}
}

Wednesday, May 3, 2017

ClassNotFoundException and NoClassDefFoundError in java

  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.
  2. java.lang.NoClassDefFoundError Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.
    If a class was present during compile time but not available in java classpath during runtime.
ClassNotFoundException is a checked Exception derived directly from java.lang.Exception class and you need to provide explicit handling for it while NoClassDefFoundError is an Error derived from LinkageError.

If you are using ClassLoader in Java and have two class loaders then if a ClassLoader tries to access a class which is loaded by another classloader will result in ClassNoFoundException.

ClassNotFoundException comes up when there is an explicit loading of class is involved by providing name of class at runtime using ClassLoader.loadClass(), Class.forName(),  while NoClassDefFoundError is a result of implicit loading of class because of a method call from that class or any variable access.

Creating mirror of BST