DistortLines.java 1008 B

1234567891011121314151617181920212223242526272829303132333435
  1. import java.io.*;
  2. import java.util.*;
  3. /**
  4. * DistortLines is like Distort but it works at line level. For info, read the comments on Distort.
  5. */
  6. public class DistortLines {
  7. public static void main(String[] args) throws Exception {
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
  9. LinkedList lines = new LinkedList();
  10. String line;
  11. while((line=reader.readLine())!=null) {
  12. if(line.isEmpty())
  13. continue;
  14. lines.add(line);
  15. }
  16. reader.close();
  17. for(int a=0; a<lines.size(); a++) {
  18. int b = (int)(Math.random() * lines.size());
  19. line = (String)lines.get(a);
  20. lines.set(a, lines.get(b));
  21. lines.set(b, line);
  22. }
  23. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"));
  24. for(Iterator lnIt = lines.iterator(); lnIt.hasNext();) {
  25. line = (String)lnIt.next();
  26. writer.write(line, 0, line.length());
  27. writer.newLine();
  28. writer.newLine();
  29. }
  30. writer.close();
  31. }
  32. }