긴 노트를 효율적으로 다루기
사용자가 더 긴 노트를 작성할수록 성능이 중요해집니다. Java의 버퍼링 클래스는 큰 텍스트를 효율적으로 읽고 쓸 수 있게 도와줍니다. 이 연습 문제에서는 BufferedReader와 BufferedWriter를 사용해 "note.txt"에 저장된 노트를 처리해 보세요.
필요한 java.io 패키지는 모두 미리 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Java의 입력/출력과 스트림
연습 안내
"note.txt"파일에 대해FileWriter를 감싸서BufferedWriter인스턴스bw를 만드세요.- 첫 번째 줄
"This is the first line"을 파일에 쓰세요. 그런 다음.newLine()으로 줄바꿈을 추가하고 두 번째 줄"This is the second line"을 쓰세요. - 파일
"note.txt"를 읽기 위해FileReader를 감싸서BufferedReader인스턴스br을 만드세요. - 파일을 한 줄씩 읽으세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class FileOperations {
public static void main(String[] args) {
try {
// Create a new instance of BufferedWriter
BufferedWriter bw = ____ ____(new FileWriter("note.txt"));
// Write the text, "This is the first line"
bw.____("This is the first line");
// Add a new line
bw.____();
// Write the second line of text, "This is the second line"
bw.____("This is the second line");
bw.close();
// Create a new instance of BufferedReader
BufferedReader br = ____ ____(new FileReader("note.txt"));
String line;
while ((line = br.____()) != null) {
System.out.println(line);
}
// Close the BufferedReader
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}