-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb01_stream.java
More file actions
64 lines (56 loc) · 1.74 KB
/
Copy pathProb01_stream.java
File metadata and controls
64 lines (56 loc) · 1.74 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
52
53
54
55
56
57
58
59
60
61
62
63
64
package java0911_stream.prob;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.RandomAccessFile;
/*
* [문제]
* input.txt 파일에는 팝송 가사가 들어있다.
* 이 파일에서 검색하고자 하는 문자열이 포함되어있는 라인의 번호와
* 가사를 콘솔에 출력하는 search(String inputFile, String searchWord)
* 메서드를 구현하시오.
*
* [프로그램 실행결과]
* 5 line : It exists to give You comfort
* 6 line : It is there to keep you warm
* 9 line : When You are most alone
* 10 line : The memory of love will bring you home
* 14 line : It invites you to come closer
* 15 line : It wants to show you more
* 17 line : And even if you lose yourself
* 20 line : will see you through
* 39 line : My memories of love will be of you
*/
public class Prob01_stream {
public static void main(String[] args) throws Exception {
search(".\\src\\java0911_stream\\prob\\input.txt", "You");
}// end main()
private static void search(String inputFile, String searchWord) {
// 여기를 구현하세요.
File file = new File(inputFile);
RandomAccessFile raf = null;
String str = null;
int line = 0;
try {
raf = new RandomAccessFile(file, "r");
while ((str = raf.readLine()) != null) {
line++;
if (str.contains(searchWord) | str.contains(searchWord.toLowerCase())) {
System.out.printf("%d line : %s\n", line, str);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// end search()
}// end class