始める無料で始める

Storing Recent Notes in Cache

To speed up access to recent notes, your app will store them in a lightweight cache file. In this task, you'll create a new file called "cache.txt" and confirm that it was successfully created. This sets the stage for saving frequently used data.

All the necessary packages from java.io have been imported for you.

この演習はコースの一部です

Input/Output and Streams in Java

コースを見る

演習の手順

  • Create a File object for "cache.txt".
  • Check if the file already exists.
  • If the file exists, delete it.
  • Create a new file named "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());  
        }  
    }  
}
コードを編集して実行