user3629216
user3629216

Reputation: 23

Java - Trying to display last modified date for the files in a folder

My problem is that when I try lastModified function on files I get an error - method 'lastModified' not found. It only allows me to use the lastModified function on folders, and I'm unsure how to fix this. This is what I have so far:

public static void main(String[] args) {

    String path = "C:/Desktop/ExampleFolder";

    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile())            
        {
            files = listOfFiles[i].getName();
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy HH:mm: a");

            if (files.endsWith(".txt") || files.endsWith(".csv") || files.endsWith(".docx")) {
                System.out.println("File Name: " + files + " , " + "Size: " + files.length() + " bytes " + " , " + "Last Modified : " + sdf.format(files.lastModified())  );
            }

        }

    }

}

Upvotes: 0

Views: 634

Answers (1)

Ga&#235;l J
Ga&#235;l J

Reputation: 15070

In your code you wrote files.lastModified() where files is a String.

Don't you mean following instead ?

listOfFiles[i].lastModified()

Upvotes: 1

Related Questions