BaşlayınÜcretsiz başlayın

Not konumlarını doğrulama

Kaydedilmiş bir nota erişmeden önce, uygulaman onun olması gereken yerde olduğunu doğrulamalı. Bu son egzersizde, belirli dosya ve klasörlerin var olup olmadığını kontrol edecek ve ilgili mesajları yazdıracaksın; tıpkı kâğıt notları dosyalamadan önce rafları kontrol etmek gibi.

Bu egzersiz, kursun bir parçasıdır

Java'da Girdi/Çıktı ve Akışlar

Kursa Göz Atın

Egzersiz talimatları

  • notes'a işaret eden notesDir adlı bir File nesnesi oluştur.
  • notes adlı yeni bir dizin oluştur.
  • notes adlı dizinin tüm içeriklerini listele.
  • "note.txt" adlı dosyanın mutlak yolunu al.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

class DirectoryManager {

    public static void main(String[] args) {
        try {
            // Create a directory
            File notesDir = ____ ____("notes");
            if (____.____()) {
                System.out.println("Directory 'notes' created successfully");
            }

            File noteFile = new File("notes/note.txt");
            if (noteFile.createNewFile()) {
                System.out.println("File 'note.txt' created successfully");
            }

            // List contents of the directory
            File[] files = ____.____();
            if (files != null) {
                for (File f : files) {
                    System.out.println("File: " + f.getName());
                }
            }
            
			// Retrieve and print the absolute path of the file
            System.out.println("Absolute Path: " + ____.____());

        } catch (Exception e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
Kodu Düzenle ve Çalıştır