File类模拟打印dir

mac2025-07-26  5

package com.cn; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class FileDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入路径:"); String path = sc.nextLine(); File file = new File(path); if(file.exists()) { print(file); }else { System.out.println("文件不存在"); } } public static void print(File file) { if(file.isFile()) { System.out.println(file.getName()); }else {//否则就是目录 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); File[] files = file.listFiles();//获取该目录下所有文件或者目录 for(File fi :files ) { //创建最后修改时间 Date dat = new Date(fi.lastModified()); //格式化时间 String date = sdf.format(dat); System.out.print(date+"\t"); if(fi.isFile()) {//如果是文件 long len =fi.length(); System.out.print("\t"+len+"\t"); }else {//如果是目录 System.out.print("<DIR>"+"\t"); } //不管是文件还是目录都要有文件名 System.out.println(fi.getName()); } } } }

打印结果:

请输入路径: c:/windows 2007-09-04 16:14:56        180000    aaRemove.exe 2019-03-19 12:52:52    <DIR>    addins 2019-08-01 06:48:54    <DIR>    appcompat 2019-10-25 17:01:04    <DIR>    apppatch 2019-11-01 08:51:10    <DIR>    AppReadiness 2019-09-25 18:44:09    <DIR>    assembly 2019-10-14 09:19:29    <DIR>    bcastdvr 2019-03-19 12:43:47        73216    bfsvc.exe 2019-03-19 19:46:02    <DIR>    BitLockerDiscoveryVolumeContents 2019-03-19 12:52:44    <DIR>    Boot 2019-11-01 14:09:08        67584    bootstat.dat 2019-03-19 12:52:44    <DIR>    Branding 2019-10-13 20:55:34    <DIR>    CbsTemp 2019-03-19 20:12:17    <DIR>    Containers 1970-01-01 08:00:00    <DIR>    CSC 2019-03-19 12:52:53    <DIR>    Cursors 2007-07-20 10:14:42        5073256    d3dx9_35.dll 2019-07-31 19:05:19    <DIR>    debug 2018-08-08 08:49:51    <DIR>    Defender 1970-01-01 08:00:00        7623    diagerr.xml 2019-03-19 12:52:44    <DIR>    diagnostics 2019-03-19 12:52:54    <DIR>    DiagTrack 1970-01-01 08:00:00        7623    diagwrn.xml 2019-03-19 19:43:03    <DIR>    DigitalLocker 2018-11-14 09:39:12    <DIR>    Downloaded Installations 2019-03-19 12:52:54    <DIR>    Downloaded Program Files 2018-12-20 08:21:54        39    dsspec.ini 2009-07-14 09:40:36        117248    dxva2.dll 2019-03-19 12:52:54    <DIR>    ELAMBKUP 2019-03-19 19:43:03    <DIR>    en-US 2019-10-13 20:50:26        4612520    explorer.exe 2019-07-31 19:04:21    <DIR>    Fonts 2019-03-19 12:52:44    <DIR>    GameBarPresenceWriter 2016-08-03 17:27:58        1717760    GdiPlus.dll 2019-03-19 12:52:44    <DIR>    Globalization 2019-07-31 18:38:10    <DIR>    Help 2019-07-31 18:31:36        1059840    HelpPane.exe 2019-03-19 12:45:38        18432    hh.exe 2019-03-19 12:52:55    <DIR>    IdentityCRL 2019-07-31 19:04:18    <DIR>    IME 2019-10-14 09:19:29    <DIR>    ImmersiveControlPanel 2019-10-29 10:22:09    <DIR>    INF 1970-01-01 08:00:00    <DIR>    InfusedApps 2019-03-19 12:52:44    <DIR>    InputMethod 2019-10-29 10:15:06    <DIR>    Installer 2018-04-21 11:16:04    <DIR>    kmsonboot 2019-03-19 12:52:56    <DIR>    L2Schemas 2019-03-19 12:52:44    <DIR>    LanguageOverlayCache 1970-01-01 08:00:00    <DIR>    LiveKernelReports 2018-04-21 11:16:20    <DIR>    LOG 2019-08-15 13:11:46    <DIR>    Logs 2019-03-19 12:52:56    <DIR>    Media 2019-03-19 12:44:30        43131    mib.bin 2019-11-01 09:16:43    <DIR>    Microsoft.NET 2019-03-19 12:52:44    <DIR>    Migration 1970-01-01 08:00:00    <DIR>    ModemLogs 2016-07-16 19:42:22        6144    normaliz.dll 2019-03-19 12:45:00        181248    notepad.exe 2019-03-19 19:44:57    <DIR>    OCR 2018-10-07 15:04:29        376    ODBC.INI 2019-09-25 18:43:34        469    ODBCINST.INI 2019-03-19 12:53:00    <DIR>    Offline Web Pages 2019-08-12 14:59:22    <DIR>    Panther 2019-03-19 12:52:44    <DIR>    Performance 2019-10-29 10:15:41        59144    PFRO.log 2019-03-19 12:52:44    <DIR>    PLA 2019-10-14 09:19:29    <DIR>    PolicyDefinitions 1970-01-01 08:00:00    <DIR>    Prefetch 2019-10-14 09:19:29    <DIR>    PrintDialog 2018-08-08 08:45:05        102400    ProductInfo.ini 2019-03-19 12:46:33        34925    Professional.xml 2019-09-17 08:06:40    <DIR>    Provisioning 2017-12-19 06:05:40        897688    py.exe 2017-12-05 20:42:22        55456    pyshellext.amd64.dll 2017-12-19 06:05:40        897688    pyw.exe 2007-02-18 20:00:00        274432    qdv.dll 2007-02-18 20:00:00        2189824    quartz.dll 2019-03-19 12:45:47        358400    regedit.exe 2019-07-31 19:15:14    <DIR>    Registration 2019-03-19 19:46:01    <DIR>    RemotePackages 2019-03-19 12:52:44    <DIR>    rescache 2019-07-31 18:38:11    <DIR>    Resources 2018-08-07 08:51:22        2113536    Rule.dat 2019-03-19 12:52:44    <DIR>    SchCache 2019-03-19 19:46:01    <DIR>    schemas 2019-03-19 19:46:01    <DIR>    security 2019-07-31 18:38:03    <DIR>    ServiceProfiles 1970-01-01 08:00:00    <DIR>    ServiceState 2019-08-15 13:32:59    <DIR>    servicing 2019-07-31 19:04:12    <DIR>    Setup 2019-07-31 18:34:48    <DIR>    ShellComponents 2019-09-17 08:06:40    <DIR>    ShellExperiences 2019-06-29 09:55:59    <DIR>    ShellNew 2019-03-19 12:52:44    <DIR>    SKB 2018-08-07 08:53:00        207096    SMSS-PFRO0724.tmp 2019-08-04 19:40:20    <DIR>    SoftwareDistribution 2019-03-19 12:52:44    <DIR>    Speech 2019-03-19 12:52:44    <DIR>    Speech_OneCore 2019-10-13 20:50:25        132096    splwow64.exe 2006-06-28 04:55:51        8192    srvany.exe 2019-03-19 12:52:44    <DIR>    System 2015-10-30 15:21:27        219    system.ini 2019-10-29 10:22:09    <DIR>    System32 2019-03-19 19:46:01    <DIR>    SystemApps 2019-10-14 09:19:30    <DIR>    SystemResources 2019-10-30 20:03:40    <DIR>    SysWOW64 2019-03-19 12:52:46    <DIR>    TAPI 2019-07-31 19:15:43    <DIR>    Tasks 2019-11-01 14:12:06    <DIR>    Temp 2019-09-17 08:06:42    <DIR>    TextInput 2019-03-19 12:52:46    <DIR>    tracing 2016-05-30 03:52:20        14539    TWAINSP_Lenovo_EasyCamera.ini 2016-05-30 03:52:20        7408    TWAINSP_Lenovo_EasyCamera.src 2019-07-31 19:06:24    <DIR>    twain_32 2019-03-19 12:46:01        64512    twain_32.dll 2018-05-27 15:20:46    <DIR>    UpdateAssistantV2 2018-10-07 15:04:25        39    vbaddin.ini 2019-03-19 12:52:46    <DIR>    Vss 2019-03-19 12:52:46    <DIR>    WaaS 2019-03-19 12:52:46    <DIR>    Web 2015-10-30 15:21:27        92    win.ini 2019-03-19 12:44:30        670    WindowsShell.Manifest 2019-11-01 14:52:55        276    WindowsUpdate.log 2019-03-19 12:46:01        11776    winhlp32.exe 2019-10-20 22:43:00    <DIR>    WinSxS 2019-03-19 19:45:55        316640    WMSysPr9.prx 2019-03-19 12:45:54        11264    write.exe 2019-03-19 19:43:04    <DIR>    zh-CN

File类

java.io.File;

File类的对象不但可以表示文件还可以表示为目录,,当创建一个文件对象后,就可以利用他来来对文件或者目录的属性进行操作,如:文件名、最后修改时间、文件大小等。

需要注意的是,File对象并不能直接对文件进行读写操作,只能查看文件的属性。

文件有大小,目录是没有大小的。

 

最新回复(0)