Preloader image
DDD

자바

파일 체크 반목문 (다중, 1개)

작성자 관리자 (admin)
조회수 92
입력일 2024-10-03 22:30:03

* 1개 파일 체크 반복문

import java.io.File;

public class WaitForFileExample {
    public static void main(String[] args) {
        // 확인할 파일 경로
        String filePath = "test.txt";
        File file = new File(filePath);

        // 파일이 존재할 때까지 루프
        while (true) {
            // 파일 존재 여부 확인
            if (file.exists()) {
                System.out.println("파일이 존재합니다: " + filePath);
                break; // 파일이 존재하면 반복문 종료
            } else {
                System.out.println("파일이 존재하지 않습니다: " + filePath);
                // 잠시 대기 (1초) 후 다시 확인
                try {
                    Thread.sleep(1000); // 1000밀리초 = 1초
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("파일 확인 완료.");
    }
}
 

* 다중 파일 체크 반복문

설명:

  • File 클래스를 사용하여 지정된 경로에 파일이 존재하는지 확인합니다.
  • file.exists() 메서드를 사용하여 파일 존재 여부를 검사합니다.
  • 파일이 존재하면 break로 반복문을 중단하고 종료
  • 파일이 존재하지 않으면 계속해서 반복 작업을 합니다.

import java.io.File;

public class FileCheckExample {
    public static void main(String[] args) {
        // 파일을 확인할 경로들
        String[] filePaths = { "test1.txt", "test2.txt", "test3.txt" };
        
        // 반복문을 통해 각 파일 경로를 확인
        for (String path : filePaths) {
            File file = new File(path);
            
            if (file.exists()) {
                System.out.println("파일 O : " + path);
                break; // 파일이 존재하면 반복문을 중단
            } else {
                System.out.println("파일 X : " + path);
            }
        }
        System.out.println("파일 확인 완료.");
    }
}