-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReaderWithLinearSearch.java
67 lines (56 loc) · 1.9 KB
/
ReaderWithLinearSearch.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ReaderWithLinearSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long start2 = System.currentTimeMillis();
int input = scanner.nextInt();
FileInputStream fis = null;
try {
fis = new FileInputStream("I:\\Repeated_Sorted_Non_Unique.txt");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
Set<Integer> numbers = new HashSet<>();
int counter = 0;
byte[] bytes = bis.readAllBytes();
String numberString = new String(bytes, StandardCharsets.UTF_8);
String[] numberSplit = numberString.split("\\r?\\n ");
boolean flag = false;
int count = 0;
for(String s : numberSplit)
{
if(!" ".equals(s) && input == Integer.valueOf(s))
{
flag = true;
count++;
}
}
if(flag)
{
System.out.println("Number exists "+count+" times");
}
else
{
System.out.println("Number doesn't exist in file");
}
} catch (IOException ex) {
ex.printStackTrace();
}
finally{
try {
fis.close();
bis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
long end2 = System.currentTimeMillis();
System.out.println("Time taken to read file: "+(end2-start2));
}
}
}