File获取功能

来源:互联网 发布:web软件开发方式 编辑:程序博客网 时间:2024/06/05 17:34
public static void main(String[] args) throws IOException {
        /**
         * * A:获取功能
            * public String getAbsolutePath():获取绝对路径
            * public String getPath():获取路径
            * public String getName():获取名称
            * public long length():获取长度。字节数
            * public long lastModified():获取最后一次的修改时间,毫秒值
            * public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
            * public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
         */
        //demo1();
File a =new File("E:\\");
/*    String [] arr =a.list();//仅获取文件和文件夹的名字
    for (String c : arr) {
        System.out.println(c);
        
    }
    File [] d =a.listFiles();//获取文件和文件夹的对象
    for (File f: d) {
        System.out.println(f);
        
    }*/
        String  [] arr =a.list(new FilenameFilter() {
            
            @Override
            public boolean accept(File dir, String name) {
                File a =new File(dir ,name);
                return a.isFile()&&a.getName().endsWith("jpg") ;
            }
        });
    for (String b : arr) {
        System.out.println(b);
        
    }
    }

    public static void demo1() {
        File a =new File("ccc.txt");
        System.out.println(a.getAbsolutePath());//获取绝对路径
        File b =new File("ccc.txt");
        System.out.println(b.getPath());//获取构造方法中穿入的路径
        File c =new File("D:\\workspace1\\day19\\ccc.txt");
        System.out.println(c.getName());//获取文件获取文件夹的名称
        System.out.println(c.length());
        Date d =new Date(c.lastModified());//获取最后一次的修改时间,毫秒值
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(sdf.format(d));
    }

阅读全文
0 0
原创粉丝点击