Spiga

How To Sort a File On the basis of the column Using Java

How To Sort a File On the basis of the column Using Java

Java Program to sort the file.
In the earlier post we have seen how to sort the CSV file. Now, we will see the generalised java code to sort the file on the basis of particular column. This code can be used for any type of files, like csv, txt etc. This can be useful to sort the big files on the basis of the column. For using this code, we need to take care of few things:
The format of the file should be like in this format:
The header or the column should be there and should be the first line of the file, else we will missed the first line.
Header/Col1: Header/Col2 : Header/Col3
a : c : e
s : a  : d
.. : .. : ...
Algorithm Used:
It’s not the rocket science, we have keep it simple and have used the simple algorithm. The each row has been considered as one java object. The column on which we have to sort is taken as the key and against this key we have the associated row data java objects as values. We sort the key and display the respective values.
For example: If we have to sort the above file on column 2, we will create two java objects which will have values:
Java@Obj1: a,c,e
Java@Obj2: s,a,d

Now, we are sorting on column 2, so for first row, key will be c and c will be mapped with the value Java@Obj1: a,c,e and for the second row: key will be a and a will be mapped with the value Java@Obj2: s,a,d. Now we will sort the keys, i.e. c and a and then we will display the mapped value for the sorted keys.

Here is the Java Class Example:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
/**
* The File Sorter Algorithm.
* @author sonkum1
*
*/
public class CsvFileSorter {
private String filename;
private int index;
private List list = new ArrayList();
private String header;
private String outputFile;
private String separatorTokenizer = ","; // By Default the Separator
// Tokenizer is ',' for CSV
// files.
/**
*
* @param filename : Path of the file
* @param index : The column number on which we want to sort the file.
*/
public CsvFileSorter(String filename, int index) {
this.filename = filename;
this.index = index;
try {
sorter();
} catch (IOException e) {
e.printStackTrace();
}}
/**
*
* @param filename : Path of the file
* @param index : The column number on which we want to sort the file.
* @param tokenizer : The column separator token like ',', ':', ';', etc.
*/
public CsvFileSorter(String filename, int index, String tokenizer, String outputFile) {
this.filename = filename;
this.index = index;
this.separatorTokenizer = tokenizer;
this.outputFile = outputFile;
try {
sorter();
} catch (IOException e) {
e.printStackTrace();
}}
public void sorter() throws IOException {
FileReader f = new FileReader(filename);
BufferedReader br = new BufferedReader(f);
String str;
header = br.readLine(); // remove the Header
System.out.println(header);
do {
str = br.readLine();
if (str != null) {
createobject(str);
}}
while (str != null);
br.close(); f.close();
writeSort();
}
private void createobject(String str) {
SortHelperClass helperClass = new SortHelperClass(getIndex(str), str);
list.add(helperClass);
}
/**
* Will create a output file.
* @throws IOException
*/
public void writeSort() throws IOException {
Collections.sort(list);
FileOutputStream output = new FileOutputStream(outputFile);
BufferedOutputStream bf = new BufferedOutputStream(output);
bf.write(header.getBytes());
String newLine = "\r\n";
bf.write(newLine.getBytes());
for (int i = 0; i < list.size(); i++) {
SortHelperClass helper = list.get(i);
bf.write(helper.getValue().getBytes());
bf.write(newLine.getBytes());
System.out.println(helper.getValue());
}
bf.flush();
bf.close();
output.flush();
output.close();
}
public String getIndex(String str) {
StringTokenizer token = new StringTokenizer(str, separatorTokenizer);
int i = 1;
while (token.hasMoreTokens()) {
if (i == index) {
return token.nextToken();
}
token.nextToken();
i++;
}
return null;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String file = "C:\\Users\\sonukumar\\Desktop\\test.txt";
new CsvFileSorter(file, 4,",",file+"_output.csv");
}}

class SortHelperClass implements Comparable {
private String key;
private String value;
public SortHelperClass(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public int compareTo(SortHelperClass o) {
if(key == null)
JOptionPane.showMessageDialog(null, "Please correct the Separator Tokenizer.");
return key.compareTo(o.key);
}}

0 comments: