public static void mergeSortedFiles(List<String> files, String outputFile)
            throws IOException{
        List<BufferedReader> readers = new ArrayList<BufferedReader>();
        Comparator<Pair<String, BufferedReader>> comparator = new Comparator<Pair<String, BufferedReader>>() {
            @Override
            public int compare(Pair<String, BufferedReader> o1, Pair<String, BufferedReader> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        };
        PriorityQueue<Pair<String, BufferedReader>> lineQueues =
                new PriorityQueue<Pair<String, BufferedReader>>(10, comparator);
        for(int i = 0; i < files.size(); i++){
            BufferedReader reader = new BufferedReader(new FileReader(files.get(i)));
            String line = reader.readLine();
            if(line != null){
                lineQueues.add(new Pair<String, BufferedReader>(line, reader));
                readers.add(reader);
            } else {
                reader.close();
            }
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
        while(lineQueues.size() > 0){
            Pair<String, BufferedReader> entry = lineQueues.poll();
            writer.write(entry.getKey());
            writer.newLine();
            if(readers.contains(entry.getValue())){
                String line = entry.getValue().readLine();
                if(line != null){
                    lineQueues.offer(new Pair<String, BufferedReader>(line, entry.getValue()));
                } else {
                    entry.getValue().close();
                    readers.remove(entry.getValue());
                }
            }
        }
        writer.flush();
        writer.close();
    }