I was given a program which should read all the files present in a given folder and give the file details such as file name, file size, created date and time, file location as output. If there any subfolders in the given folder, then it should geive the details of the files present in that subfolder too.
Asked
Active
Viewed 197 times
-3
-
There is an API for that in standard Java library. What exactly is your question? – Forketyfork Jun 08 '15 at 11:55
-
You can use `File.list()` method take a look at https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles(java.io.FileFilter)https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles(java.io.FileFilter) – Kenneth Clark Jun 08 '15 at 12:00
1 Answers
0
package FileDetails;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;``
public class FileDetails
{
public static void filelist(final File folder) throws IOException
{
for (final File fileEntry : folder.listFiles())
{
if(fileEntry.isDirectory())
{
filelist(fileEntry);
}
else
{
//To get file name
System.out.println("File name is : " + fileEntry.getName());
//To get file extension
String fileName = fileEntry.getName();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
{
System.out.println("Extension of file is : " + fileName.substring(fileName.lastIndexOf(".")+1));
}
//To get file size
double bytes = fileEntry.length();
double kilobytes = (bytes / 1024);
System.out.println("Size of the file is : " + kilobytes + " KB");
//To get file times
Path path = fileEntry.toPath();
BasicFileAttributes attr = Files.readAttributes(path,BasicFileAttributes.class);
System.out.println("Creation time : " + attr.creationTime());
System.out.println("Last Access time : " + attr.lastAccessTime());
System.out.println("Last Modified time : " + attr.lastModifiedTime() + "\n\n");
}
}
}
public static void main(String[] arg) throws IOException
{
final File folder = new File("folder location");
filelist(folder);
}
}

Ashruth
- 11
- 2