시작하기무료로 시작하기

메모 위치 확인하기

저장된 메모에 접근하기 전에, 앱이 메모가 있어야 할 위치에 있는지 확인해야 해요. 이 마지막 연습 문제에서는 특정 파일과 폴더가 존재하는지 확인하고, 종이 메모를 정리하기 전에 서가를 확인하듯 관련 메시지를 출력해 보겠습니다.

이 연습은 강의의 일부입니다

Java의 입력/출력과 스트림

강의 보기

연습 안내

  • notes를 가리키는 File 객체 notesDir을(를) 생성하세요.
  • notes라는 새 디렉터리를 생성하세요.
  • notes라는 디렉터리의 모든 내용을 나열하세요.
  • 이름이 "note.txt"인 파일의 절대 경로를 가져오세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

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());
        }
    }
}
코드 편집 및 실행