Advertisement
cd62131

Remove Same Lines

Feb 2nd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.76 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.Writer;
  4. import java.util.Scanner;
  5.  
  6. public class RemoveSameLines {
  7.   public static void main(String[] args) {
  8.     if (args.length < 2) {
  9.       System.out.println("IN_FILE OUT_FILE");
  10.       System.exit(1);
  11.     }
  12.     Scanner in = null;
  13.     Writer out = null;
  14.     String prev = "";
  15.     try {
  16.       in = new Scanner(new File(args[0]));
  17.       out = new FileWriter(new File(args[1]));
  18.       StringBuffer sb = new StringBuffer();
  19.       while (in.hasNextLine()) {
  20.         String cur = in.nextLine();
  21.         if (prev.equals(cur)) continue;
  22.         sb.append(cur);
  23.         sb.append("\n");
  24.         prev = cur;
  25.       }
  26.       out.write(sb.toString());
  27.     }
  28.     catch (Exception e) {
  29.     }
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement