/*************************************************************************** * Anastasios Monachos - anastasiosm@gmail.com * * Compile: javac LineShuffler.java * * Usage: java LineShuffler inputfile * * The program will accept as argument the name of file, which will load its contents * in a List and randomly will shuffle its data, saving the results in a new file called * inputfile.shuffled.txt ***************************************************************************/ import java.io.*; import java.lang.*; import java.util.ArrayList; import java.util.List; import java.util.Collections; public class LineShuffler { private static int TOTAL_LINES_IN_INPUT_FILE = 0; private static String outputFile = ".shuffled.txt"; private static List lines = new ArrayList(); public static void main(String args[]) { // Check the arguments if ((args.length!=1)) { help(); } else { new LineShuffler(args[0]); System.out.println("Shuffled data must have been saved into: "+args[0]+outputFile); } }//end of main public LineShuffler(String inputfile) { //first find how many lines the file has got count(inputfile); //Load all lines of file in ArrayList try { FileReader file = new FileReader(inputfile); BufferedReader buffer = new BufferedReader(file); String line = null; while ((line = buffer.readLine()) != null) { lines.add(line); } buffer.close(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } //Now do shuffling Collections.shuffle(lines); //System.out.println("After shuffling, ArrayList contains : " + lines);//ok in terms of size //Now attempt to save shuffled data to output file try { File f = new File(inputfile+outputFile); FileWriter fwriter = new FileWriter(f, true);//true, so to append BufferedWriter bwriter = new BufferedWriter(fwriter); for (int i=0; i