Add Files to a Collection: pseudocode included inside

Reply

Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Add Files to a Collection: pseudocode included inside

 
0
  #1
May 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Add Files to a Collection: pseudocode included inside

 
0
  #2
May 22nd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Add Files to a Collection: pseudocode included inside

 
0
  #3
May 23rd, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: Add Files to a Collection: pseudocode included inside

 
0
  #4
May 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Add Files to a Collection: pseudocode included inside

 
0
  #5
May 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: pmalhotra_10 is an unknown quantity at this point 
Solved Threads: 0
pmalhotra_10 pmalhotra_10 is offline Offline
Newbie Poster

Re: Add Files to a Collection: pseudocode included inside

 
0
  #6
May 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Add Files to a Collection: pseudocode included inside

 
0
  #7
May 23rd, 2009
You can use pmalhotra's error-prone difficult version with the unchecked run-time cast:
  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

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

I know which I prefer.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: Add Files to a Collection: pseudocode included inside

 
0
  #8
May 24th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC