Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectOutputStream;
  6. import java.io.PrintStream;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.Scanner;
  10.  
  11. public class Test {
  12. public static void main (String args[]) throws IOException{
  13. Scanner sc2 = null;
  14. HashSet withoutDuplicatesHashSet = new HashSet();
  15. try {
  16. sc2 = new Scanner(new File("sample.txt"));
  17.  
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. while (sc2.hasNextLine()) {
  22. Scanner s2 = new Scanner(sc2.nextLine());
  23. boolean b;
  24. while (b = s2.hasNext()) {
  25. String s = s2.next();
  26. withoutDuplicatesHashSet.add(s);
  27. System.out.println(s);
  28. }
  29. }
  30. System.out.println(withoutDuplicatesHashSet);
  31.  
  32. PrintStream out = new PrintStream(new FileOutputStream("WithoutDuplicates.txt"));
  33. Iterator hashSetIterator = withoutDuplicatesHashSet.iterator();
  34. while(hashSetIterator.hasNext()){
  35. out.println(hashSetIterator.next());
  36. }
  37.  
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement