943,670 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 811
  • Java RSS
May 22nd, 2009
0

Add Files to a Collection: pseudocode included inside

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 22nd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 23rd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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:
java Syntax (Toggle Plain Text)
  1. for (File f : arrayList ) {
  2. if (f.length() <2048) System.out.println(f.getName());
  3. }
Last edited by JamesCherrill; May 23rd, 2009 at 6:08 am.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
May 23rd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
May 23rd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
May 23rd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pmalhotra_10 is offline Offline
3 posts
since May 2009
May 23rd, 2009
0

Re: Add Files to a Collection: pseudocode included inside

You can use pmalhotra's error-prone difficult version with the unchecked run-time cast:
Java Syntax (Toggle Plain Text)
  1. for(int i=0; i<arrayList.size();i++){
  2. File n= (File)arrayList.get(i);
  3. System.out.println(n.length());
  4. }

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

Java Syntax (Toggle Plain Text)
  1. for (File n : arrayList) {
  2. System.out.println(n.length());
  3. }

I know which I prefer.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,765 posts
since Apr 2008
May 24th, 2009
0

Re: Add Files to a Collection: pseudocode included inside

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
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Bus Information System
Next Thread in Java Forum Timeline: Need titlein title bar to be removed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC