ArrayList arrayList = new ArrayList();           

for (File g : f.listFiles())

if (g.isFile()) 

    {

            arrayList.add(g);

    }

I hope from the code above you can understand what Im trying to do. If a file is a file then add it to an arraylist. I dont know if this is the best one, its the one I know most. After adding it to an array, collection etc I would like to further process the files. But all I want help with is being able to extract the files from the list, eg all files that have equal size. So how can I go about this using my code as the basis? I done:

System.out.println (arrayList.toArray());

But that printed off some object references rather than filenames

So is arraylist not appropirate? and if so how can go about processing the files. eg listing all the files in the array that are equal in size

Recommended Answers

All 7 Replies

To get the size of a file, you use file.length(). This tells you how many bytes the file is. Is that all you needed to know?

ArrayList, from the info you have given, is a very good choice. Wen you declare and create it, specify the type of object it's supposed to contain - this will allow the compiler to check your code better and will simplify subsequent processing of its contents. ArrayList<File> arrayList = new ArrayList<File>(); Now you will a;so be able to loop thru it using the improved loop syntax that you used with listFiles(), eg:

for (File f : arrayList ) {
   if (f.length() <2048) System.out.println(f.getName());
}

thanks james, the code works fines with the rest of my code, but to be honest i dont really understand what difference it made, because the final results when executing my program are the same as to when I didnt explicity create an ArrayList.


What I was hoping it would do was enable me to ready binary data of files in two folders at a parallel level in the folder arcitecture.

Because at the moment, Im able to compute files from say Folder A and Folder B, so files in Folder A & B are computed independtly. I want to be able to put files from Folder A and files from Folder B into an arrayList X, and then I want to be able to compute the files in X as if they were in one folder and i could listFiles().

I dont know if ive conveyed what im trying to gt at, if some ones got a little free time and is able to review the code ill be happy to PM it.

The addition of the <File> won't make a dramatic difference, but it will simplify processing the data (eg you won't have to do a runtime cast every time you access a member of the list).
Putting both directories into one ArrayList won't tax you too hard... just create a new ArrayList, add all the files from directory1, then add all the files from directory2.

U must get the File object from the arraylist and then carry out any other operation on them.
Somethg like this:

for(int i=0; i<arrayList.size();i++){
    File n= (File)arrayList.get(i);
    System.out.println(n.length());

}

Hope this helps

You can use pmalhotra's error-prone difficult version with the unchecked run-time cast:

for(int i=0; i<arrayList.size();i++){
File n= (File)arrayList.get(i);
System.out.println(n.length());
}

or, if you have Java 1.5 or later you can use the enhanced for loop

for (File n : arrayList) {
  System.out.println(n.length());
}

I know which I prefer.

lol i know which i prefer.


I get files from one or more directories, but im only able to compare each file with files from its local directory, i think perpaps wrong ordering of statements, i dont know im on a break now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.