'Need to return metadata rather than directory

Needs to output the metadata ideally to a text document rather than just the console but can do this part by myself if i manage to get the console working first.

package cmd; 

import java.nio.file.attribute.BasicFileAttributes; 
import java.util.Scanner; 
import java.nio.file.Files; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.Path; 
import java.nio.file.Paths;
    
public class cmd { 
    static String pattern = ""; 
    
    public static void main(String[] args) {
        System.out.println("Please enter the filepath you would like to scan e.g. C,D,M");
        Scanner s =new Scanner(System.in);
        String filePath = s.next().toUpperCase()+":\\";
        fileType();
        walk(new File(filePath ));
    }

    //public class getData{
    public void getData(String file) throws IOException{
        Path path = Paths.get(file);
        BasicFileAttributes metadata = Files.readAttributes(path, BasicFileAttributes.class, null);
    
        this.creationTime = metadata.creationTime().toString();
        this.accessTime = metadata.lastAccessTime().toString();
        this.modifiedTime = metadata.lastModifiedTime().toString();
        this.regularFile = metadata.isRegularFile();
        this.fileSize = metadata.size();
        this.fileName = file;
    }
    
    public static void fileType() {
        Scanner input = new Scanner(System.in);
        int fileType;
        System.out.println("Please select a filetype");
        System.out.println("1 - .exe");
        System.out.println("2 - .docx");
        System.out.println("3 - .txt");
        System.out.println("4 - .bat");
    
        fileType = input.nextInt();
        if(fileType>0 && fileType<5) {
            switch(fileType) {
    
            case 1:{
                pattern = ".exe";
                break;
            }    
    
            case 2:{
                pattern = ".docx";
                break;
            }
    
            case 3:{
                pattern = ".txt";
                break;
            }
    
            case 4:{
                pattern = ".bat";
                break;
            }
            }
        }
        else {fileType();}
    }
    String creationTime;
    String accessTime;
    String modifiedTime;
    boolean regularFile;
    long fileSize;
    String fileName;
    
    public String toString() {
        return String.format("%s\n"
                +"File created at (yyyy/mm/dd)(hh/mm/ss):%s\n"
                +"File was last accessed at (yyyy/mm/dd)(hh/mm/ss):%s\n"
                +"File was last modified at (yyyy/mm/dd)(hh/mm/ss):%s\n"
                +"Is this file considered regular (True/False):%s\n"
                +"File size:%s",fileName,creationTime,accessTime,modifiedTime,regularFile,fileSize);
    
    }

    //  public void listFile(String fileName,String creationTime, String accessTime, String modifiedTime, boolean regularFile, long fileSize) {}
    public static void walk(File dir) {
        File listFile[]=dir.listFiles();
        if (listFile !=null) {
            for (int i=0;i<listFile.length;i++) {
                if (listFile[i].isDirectory()) {
                    walk(listFile[i]);      
                }
                else {
                    if (listFile[i].getName().endsWith(pattern)) {
                        System.out.println(listFile[i].toString());
                    }
                }
            }
        }
    }
}

So basically I'm trying to return the metadata from my file walker but currently its just outputting the directory and I have no clue how to fix this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source