> Erlang中文手册 > read_file_info/2 获取一个文件信息

file:read_file_info/2

获取一个文件信息

用法:

read_file_info(Filename, Opts) -> {ok, FileInfo} | {error, Reason}

获取文件 Filename 的文件信息 FileInfo,例如文件的大小,创建时间,最后修改时间。FileInfo 其实是一个 file_info 的 record 记录,记录字段信息如下:

-record(file_info,
        {size   :: non_neg_integer(),   % Size of file in bytes.
         type   :: 'device' | 'directory' | 'other' | 'regular' | 'symlink',
         Access :: 'read' | 'write' | 'read_write' | 'none',
         atime  :: file:date_time() | non_neg_integer(),
                                     % The local time the file was last read:
                                     % {{Year, Mon, Day}, {Hour, Min, Sec}}.
                                     % atime, ctime, mtime may also be unix epochs()
         mtime  :: file:date_time() | non_neg_integer(),
                                     % The local time the file was last written.
         ctime  :: file:date_time() | non_neg_integer(),
                                     % The interpretation of this time field
                                     % is dependent on operating system.
                                     % On Unix it is the last time the file
                                     % or the inode was changed.  On Windows,
                                     % it is the creation time.
         mode   :: non_neg_integer(), % File permissions.  On Windows,
                                     % the owner permissions will be
                                     % duplicated for group and user.
         links  :: non_neg_integer(),
                                     % Number of links to the file (1 if the
                                     % filesystem doesn't support links).
         major_device :: non_neg_integer(),
                                     % Identifies the file system (Unix),
                                     % or the drive number (A: = 0, B: = 1)
                                     % (Windows).
         %% The following are Unix specific.
         %% They are set to zero on other operating systems.
         minor_device :: non_neg_integer(), % Only valid for devices.
         inode   :: non_neg_integer(),       % Inode number for file.
         uid     :: non_neg_integer(),       % User id for owner.
         gid     :: non_neg_integer()}).     % Group id for owner.

参数 Opts 定义一些返回数据的格式,例如要定义字段 atime、mtime 和 ctime 的数据格式,参数的 Opts 的格式可以写成这样 {time, Type},Type 的值有:

  • local: 返回一个本地时间
  • universal: 返回一个通用时间
  • posix: 返回一个 1970-01-01 00:00 到现在的时间戳

默认的时间格式是返回本地时间({time, local}), 下面是规定返回的时间格式是一个时间戳:

file:read_file_info("./rebar.config", [{time, posix}]).