java: file

 

/**
 * 版權所有 2022 塗聚文有限公司
 * 許可信息查看:
 * 描述:
 * IDE:IntelliJ IDEA 2021.2.3
 * 數據庫:MSSQL Server 2019
 * OS:windows 10 x64
 * 歷史版本: JDK 14.02
 * 2022-1-12 創建者 geovindu
 * 2022-1-15 添加 Lambda
 * 2022-1-15 修改:date
 * 接口類 mssql-jdbc-9.4.1.jre16.jar.
 *
 * 2022-1-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc Filter.java

 * */


package Geovin.Common;

import java.io.File;
import java.io.*;
import java.io.FilenameFilter;
import java.util.*;

/**
 * 自定義文件擴展名過濾器
 * @author geovindu
 * @version 1.0
 *
 * */
public class Filter implements FilenameFilter {

    String extent;

    public Filter(String extent)
    {
        this.extent=extent;
    }
    /**
     *
     * @param dir 文件夾 名
     * @param name  文件擴展名
     * @return  bool
     *
     * */
    @Override
    public  boolean Accept(File dir,String name)
    {
        return  name.endsWith("."+extent);

    }
}

  

 

 //Geovin Du geovindu
        File dir=new File("./TestDir");
        String ext="html";
       Geovin.Common.Filter filter=new Filter(ext);
       System.out.println(""+dir);
       String files[]=dir.list(filter);
       for(String fileName:files)
       {
           File file=new File(dir,fileName);
           if(file.isFile())
           {
               System.out.println("文件名:"+ file.getName());
               System.out.println("文件絕對路徑:"+file.getAbsolutePath());
               System.out.println("文件路徑:"+file.getPath());
               System.out.println("文件最後修改時間:"+file.lastModified());
               System.out.println("文件大小:"+file.length());
               System.out.println("是否存:"+file.exists());
               System.out.println("是否可寫:"+file.canWrite());

           }
           else
           {
               System.out.println("子目錄:"+file);
           }
       }

        Handler consoleHandler = null;
        Handler fileHandler  = null;
        try{
            //Creating consoleHandler and fileHandler
            consoleHandler = new ConsoleHandler();
            fileHandler  = new FileHandler("./javacodegeeks.log");

            //Assigning handlers to LOGGER object
            LOGGER.addHandler(consoleHandler);
            LOGGER.addHandler(fileHandler);

            //Setting levels to handlers and LOGGER
            consoleHandler.setLevel(Level.ALL);
            fileHandler.setLevel(Level.ALL);
            LOGGER.setLevel(Level.ALL);

            LOGGER.config("Configuration done.");

            //Console handler removed
            LOGGER.removeHandler(consoleHandler);

            LOGGER.log(Level.FINE, "Finer logged");
        }catch(IOException exception){
            LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
        }

        LOGGER.finer("Finest example on LOGGER handler completed.");

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章