최근 노트를 캐시로 저장하기
최근 노트에 더 빨리 접근하기 위해 앱이 가벼운 캐시 파일에 저장하려고 합니다. 이 과제에서는 "cache.txt"라는 새 파일을 만들고, 성공적으로 생성되었는지 확인해 보세요. 이는 자주 사용하는 데이터를 저장하기 위한 준비 단계입니다.
필요한 java.io 패키지는 모두 미리 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Java의 입력/출력과 스트림
연습 안내
"cache.txt"에 대한File객체를 생성하세요.- 파일이 이미 있는지 확인하세요.
- 파일이 있으면 삭제하세요.
"cache.txt"라는 새 파일을 생성하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class CacheDataManager {
public static void main(String[] args) {
try {
// Create a File object
File cacheFile = ____ ____("cache.txt");
// Check if the file exists
if (____.____()) {
// Attempt to delete the file
if (____.____()) {
System.out.println("Old cache file deleted successfully.");
} else {
System.out.println("Failed to delete the old cache file.");
}
}
// Create the file for cache.txt
if (____.____()) {
System.out.println("New cache file created successfully.");
} else {
System.out.println("Failed to create the new cache file.");
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}