forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
37 lines (29 loc) · 869 Bytes
/
RemoveDuplicates.java
File metadata and controls
37 lines (29 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package filehandling;
/**
* use HashSet to store each line of input.txt.
* As set ignores duplicate values, so while storing a line,
* check if it already present in hashset. Write it to output.txt
* only if not present in hashset.
*/
import java.io.*;
import java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) throws IOException {
try (
PrintWriter pw = new PrintWriter("Output.txt");
BufferedReader br = new BufferedReader(new FileReader("file.txt"));) {
String line = br.readLine();
// Set store unique values
HashSet<String> hs = new HashSet<>();
while(line != null) {
if(hs.add(line)) {
pw.println(line);
}
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File operation performed successfully");
}
}