java 파일입출력 간단한 단어검색 만들기

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Word_Search {

public static void main(String[] args) throws IOException {
String str;
int count = 0;
boolean flag=false;

Scanner sc =new Scanner(System.in);
String input;
BufferedReader in = null;
BufferedWriter out = null;

while(true){
System.out.println("1. 단어입력  2. 단어검색  3. 종료  :");
System.out.print(">>");
input = sc.next(); //1 ="1"

switch(input.charAt(0)){//문자열에서 0번째 위치한 한개만
case '1':

System.out.print("단어/뜻 입력>>");

in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine(); //apple/사과

out = new BufferedWriter(new FileWriter("word.txt",true));

out.write(str);
out.write("\r\n"); //out.newLine();과 같다

System.out.println();
out.close(); // 이 줄 없으면 입력한 단어 저장이 안된다.
break;

case '2':
System.out.println("검색 단어 입력>>");

in = new BufferedReader( new InputStreamReader(System.in));

str = in.readLine(); //apple

in = new BufferedReader( new FileReader("word.txt"));
String line;

while((line = in.readLine()) != null){ //apple/사과
flag= false;
String[] tokens = line.split("/");
//token[0] = apple, tokens[1] = 사과

if(tokens[0].trim().compareToIgnoreCase(str.trim()) == 0){ // 같으면 0 다르면 0이아닌 다른 숫자
System.out.println("단어>" + tokens[0].trim());
System.out.println("단어의 뜻>" + tokens[1]);
flag= true;
break;
}
}
// 여기
if(!flag){
System.out.println("찾는 단어 없음");
}
System.out.println();

break;
case '3':
System.out.println("프로그램 종료합니다.");
in.close();
System.exit(0);
}
}
}

}

                            결과




댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About idempotent

About G1 GC

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

sneak peek jitpack

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

DDD(Domain Driven Design) - Aggregate (어그리게잇)

Strategy Pattern In Spring (feat. JPA)