最近のメモをキャッシュに保存する
最近のメモへのアクセスを高速化するため、アプリは軽量なキャッシュファイルに保存します。このタスクでは、"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());
}
}
}