從目錄中查找最大和最小的文件(不包括子目錄)

從目錄中查找最大和最小的文件(不包括子目錄),學會使用File類的一些基本方法的調用。代碼如下:

package file;

import java.io.File;

public class FindMinAndMaxFile {
    public static void main(String[] args) {
        File windows = new File("c:/windows");
        File[] fs = windows.listFiles();
        int min = -1;
        int max = -1;

        int j = min + 1;
        while (j < fs.length) {
            if (fs[j].isFile() && fs[j].length() > 0 && min < 0) {
                min = max = j;
                j++;
                continue;
            }
            if (fs[j].isFile() && (fs[j].length() != 0)) {
                if (fs[j].length() < fs[min].length())
                    min = j;
                if (fs[j].length() > fs[max].length())
                    max = j;
            }
            j++;
        }

        System.out.println("the min file is:" + fs[min].getName() + " and it's length is " + fs[min].length());
        System.out.println("the max file is:" + fs[max].getName() + " and it's length is " + fs[max].length());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章