Help with accessing ArrayList
For a class project we are suppose to create code that once given a directory will return all files in said directory in a order from most recently modified. I have decided to use an array list that will store objects that can be used to refer to both the name and date of a file. This way i can order the objects correctly and then print out their names. My problem is placing the objects into the array after they have been created in a way that allows me to retrieve them. The code i currently have places them into the array, and upon debugging shows that all the correct info is in the array, but i cannot access this data in the main. It says "cannot find symbol - method getFileName().
I'm not sure if im doing something that will not work, but if there are any small tweaks i can make to access the data I need it will be greatly appreciated. Thank you so much in advance!
This is what I have so far, have not worked on ordering them yet:
Code:
import java.io.*;
import java.util.*;
public class Pch20_x1ab{
public static ArrayList<Object> FileList = new ArrayList<Object>();
public static void main(String[] args) {
System.out.print("Enter a directory or a file: ");
Scanner input = new Scanner(System.in);
File directory = new File(input.nextLine());
if (directory.exists())
getFileName(directory);
else System.out.println("Error");
Object x = FileList.get(1);
System.out.println(x.getFileName());
}
public static void getFileName(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++){
if (files[i].isDirectory())
getFileName(files[i]);
else {
MakeFile x = new MakeFile(files[i]);
FileList.add(x);
System.out.println(x.getFileName());
}
}
}
else{
FileList.add(new MakeFile(file));
}
}
}
class MakeFile{
long dateCreated;
String fileName;
MakeFile(){
dateCreated = 0;
fileName = "";
};
MakeFile(File files){
fileName = files.getPath();
dateCreated = files.lastModified();
}
String getFileName(){
return fileName;
}
long getDate(){
return dateCreated;
}
}