FileInputStream : 파일에서 바이트 단위로 읽기
FileOutputStream : 파일에 바이트 단위로 쓰기
try-with-resources: 블록 종료 시 자동 close()
read() : 1바이트 읽기, 파일 끝이면 -1 반환
write() : 1바이트 또는 byte[] 쓰기
getBytes() : String → byte[] 변환
(char) : int → 문자 형변환
append 모드 : new FileOutputStream("파일", true)
-타자 연습 기록기 코드
package io.ch15_1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class TypingRecord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("===타자 연습 기록기===");
System.out.println("1. 문장 저장");
System.out.println("2. 기록 보기");
System.out.print("선택 : ");
String choice = sc.nextLine();
if (choice.equals("1")) {
saveRecord(sc);
} else if (choice.equals("2")) {
printRecord();
}
sc.close(); // 메모리 누수 방지
} // end of main
private static void printRecord() {
System.out.println("\n===저장된 기록 ===");
try (FileInputStream fin = new FileInputStream("typing_record.txt")) {
int data = 0;
while ((data = fin.read()) != -1) {
System.out.println((char) data);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void saveRecord(Scanner sc) {
System.out.print("연습한 문장을 입력하세요 : ");
String input = sc.nextLine();
try (FileOutputStream fos = new FileOutputStream("typing_record.txt", true)) {
fos.write(input.getBytes());
// 줄바꿈 추가
fos.write("\n".getBytes());
System.out.println("저장 완료!");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
현재 기록 보기 기능은 저장된 내용을 그냥 출력함. 아래 기능을 추가 해보기
현재 출력: 개선 후 출력:
The quick brown fox 1번 기록 : The quick brown fox
jumps over the lazy 2번 기록 : jumps over the lazy
dog 3번 기록 : dog
총 3개의 기록이 있습니다.
-코드
package io.ch15_1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class TypingRecord2 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("===타자 연습 기록기===");
System.out.println("1. 문장 저장");
System.out.println("2. 기록 보기");
System.out.print("선택 : ");
String choice = sc.nextLine();
if (choice.equals("1")) {
saveRecord(sc);
} else if (choice.equals("2")) {
printRecord();
}
sc.close(); // 메모리 누수 방지
} // end of main
private static void printRecord() throws Exception {
System.out.println("\n===저장된 기록 ===");
try (FileInputStream fin = new FileInputStream("typing_record.txt")) {
int data;
int lineNumber = 1; //현재 출력 중인 줄 번호
StringBuilder sb = new StringBuilder();
//StringBuilder: 문자를 하나씩 이어붙이는 가변 문자열 버퍼
//String += "가"를 반복하면 매번 새로운 객체가 생겨 느리므로 StringBuilder사용
while ((data = fin.read()) != -1) {
System.out.print((char) data);
// 출력할 때 만약 \n(개행문자) 이 들어온다면 카운트를 1씩 올리겠다
if ((char) data == '\n') {
//개행 문자 (\n) 만났다 == 한 줄이 끝났다
lineNumber++;
} else {
//개행문자(\n) 아니라면 sb에 계속 이어붙임
sb.append((char) data);
}
}
//출력할 때 만약 \n(개행문자) 이 들어온다면 카운트를 1씩 올린다
System.out.println("\n" + sb.toString());
System.out.println("총 " + lineNumber + "개의 기록이 있습니다");
}
}
private static void saveRecord(Scanner sc) {
System.out.print("연습한 문장을 입력하세요 : ");
String input = sc.nextLine();
try (FileOutputStream fos = new FileOutputStream("typing_record.txt", true)) {
fos.write(input.getBytes());
// 줄바꿈 추가
fos.write("\n".getBytes());
System.out.println("저장 완료!");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}