Bài 208: Check thư mục rỗng trong java

Ngày đăng: 1/6/2023 10:17:50 AM

Ví dụ check thư mục rỗng trong java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17    

18

19

import java.io.File;

 

public class CheckFolderEmptyExample {

    public static void main(String[] args) {

        File file = new File("D:\testout");

        if (file.isDirectory()) {

            // check thu muc rong bang phuong thuc file.list()

            if (file.list().length == 0) {

                System.out.println("Thu muc "

                        + file.getAbsolutePath() + " la rong!");

            } else {

                System.out.println("Thu muc "

                        + file.getAbsolutePath() + " khong rong!");

            }

        } else {

            System.out.println(file.getAbsolutePath() + " khong phai la thu muc!");

        }

    }

}

Output:

 Thu muc D:	estout la rong!

Nguồn tin: viettuts.vn