0 and 1’s

Unix Command uniq in Java

Posted in Java by Rama Krishna on March 16, 2008

Today, i encountered a situation where i needed to get unique lines from a file. On Unix/Linux, this can be done easily using the system built in command ‘uniq’. But, i was on Windows. I am very poor with DOS commands, and didn’t even bother to check DOS documentation to check if such a utility existed. I used to use Unix commands in Windows using the “Unix commands in windows” utility. It didn’t have the uniq tool. I developed a similar utility using Java. The actual code is about 10 lines.
The tool reads every line from a file and loads it to a LinkedHashSet and then writes the contents to a file.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class GetUniqueLines {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        Set<String> s = new LinkedHashSet<String>();
        String line = null;
        while ((line = br.readLine())!= null)
            s.add(line);
        Iterator<String> it = s.iterator();
        BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
        while (it.hasNext()) {
            bw.write(it.next());
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}

8 lines of imports for 12 lines of code. :)

Tagged with: , ,