1

I want to get additional information about files using Java NIO. I use this Java code to get basic information:

private List<LogObj> generateFilesList()
    {
        List list = new ArrayList();
        File[] files = new File("/ot").listFiles();

        for (File file : files)
        {
            if (file.isFile())
            {
                LogObj obj = new LogObj(file.getName(), null, file.length());
                list.add(obj);
            }
        }
        return list;
    }

How I can get additional information about file create date?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Also voting "close as duplicate". Question says "using NIO" and is tagged with "nio", but does not explain that bit, so it does probably not actually matter to OP. – Hugues M. Jun 24 '17 at 13:05

2 Answers2

2

Just change your code to this:

private List<LogObj> generateFilesList()
{
    List list = new ArrayList();
    File[] files = new File("/ot").listFiles();

    for (File file : files)
    {
        if (file.isFile())
        {
            LogObj obj = new LogObj(file.getName(), null, file.length());
            list.add(obj);
            BasicFileAttributes attributes = Files.readAttributes(Paths.get(file.toURI()), BasicFileAttributes.class);
            FileTime fileTime = attributes.creationTime();
            Date date = new Date(fileTime.toMillis());
            System.out.println(date);
        }
    }
    return list;
}
dabaicai
  • 999
  • 8
  • 9
0

Please have a look at the API description https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html

Florian S.
  • 386
  • 1
  • 12