• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unsorted file list

 
Mukesh Sehgal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heh guys,
I am having a little problem with uploading files onto the server. Initially when I try to upload the files, they go to a particular directory... until this point everything works fine. Now when I want to display the contents of the directory, I am taking the contents out of that directory into an array and displaying that array. Now the problem is that when I try to display the array, the contents are not in the same order as they were entered into the directory.
here is the sample code:

String DirPath = PDF_UPLOAD + "Unzip" + uname;
File temp = new File(DirPath);
String[] fileNames = temp.list();

Problem is I am using the list to read the file(which is nothing but the directory)... so does anyone knows anyother way to do it rather than list???
or any kind of solution to this??
I even tried to sort this array but I want it to sort the way they were entered???
java.util.Arrays.sort(fileNames, String.CASE_INSENSITIVE_ORDER);
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the I/O forum.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to compare the files by the time they were last modified, the file name is not going to be enough information. The File class has a lastModified() method which returns the last modified time as a long, so you need File instances. File.listFiles() will return an array of File instances. Now, how to compare? Arrays.sort() can take a Comparator argument in addition to an array, so it's up to you to create a Comparator which compares two File instances by date.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!