-
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;
}
}
Similar Threads
-
Replies: 3
Last Post: 08-22-2007, 01:58 PM
-
By jawolken in forum Java
Replies: 3
Last Post: 10-11-2006, 03:30 PM
-
Replies: 1
Last Post: 03-06-2006, 04:36 PM
-
By Gengar003 in forum Java
Replies: 10
Last Post: 05-15-2005, 06:14 AM
-
By Jason Salas in forum ASP.NET
Replies: 4
Last Post: 07-31-2003, 08:40 PM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|