-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrep.java
More file actions
51 lines (46 loc) · 1.63 KB
/
Grep.java
File metadata and controls
51 lines (46 loc) · 1.63 KB
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
// KFD-Grep/Grep.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Grep {
public static void main(String[] args) {
File filein = new File("alice.txt");
String wordSearchedFor = "Cheshire";
if ( !filein.exists() ||
!filein.canRead() ||
filein.isDirectory() ) {
System.out.println("Invalid input file !!!");
System.exit(1);
}
File fileou = new File("grep_" + filein.getName());
BufferedReader br = null;
BufferedWriter bw = null;
String LF=System.getProperty("line.separator");
try {
br = new BufferedReader(
new FileReader(filein));
bw = new BufferedWriter(
new FileWriter(fileou));
String line;
int lineNo = 0;
while ( (line = br.readLine()) != null) {
++lineNo;
if (line.indexOf(wordSearchedFor) >=0)
bw.write(String.format("Line %2d: %s%s",
lineNo,line,LF));
}
} catch (IOException e) {
System.out.println("Problems with reading");
} finally {
try { if (br != null) br.close(); }
catch(IOException ignore) { }
try { if (bw != null) bw.close(); }
catch(IOException ignore) { }
System.out.println("Results written to " +
fileou.getAbsolutePath());
}
}
}