Reputation: 365
I know how to get files in the folder. But when the list of files is returned it is not in the same order as present in folder. It is in some other order. So can anyone explain me how to get them in the same manner they are present in folder? By the way above stated everything is related with java.
Upvotes: 0
Views: 2070
Reputation: 20875
The files belongs to a directory, and there's no order defined. Different programs can filter/order the results on explicit user request - think of file managers and the output of command-line utilities like ls
or dir
.
You have to order the files in your program depending on your needs/user criteria. You may find the metadata API more useful than the old File
-based one
Upvotes: 0
Reputation: 70909
You sort them.
The program that presents the folder has a sorting routine, and probably a configurable sorting routine. You need to determine the sort order and then duplicate it in your code.
If you want to keep them in the same sorted order forever, you cannot easily find out the latest sort order in the folder without communicating with the folder.
Some people approach this by attempting to figure out and read the desktop's internal configuration details, others by embedding the desktop into their application, and a few by attempting to communicate with the desktop as a separate entity. They all have their advantages; but, all these techniques mean more code and a more complicated application.
Upvotes: 0
Reputation: 7320
Files do not have a fixed order in folders. The order is always arranged by e.g. the file manager. Thus, you can set e.g. the Windows Explorer to order the files by name, date, etc..
If you want you want your files to be e.g. alphabetically sorted by file name, see e.g. this SO posting.
Upvotes: 1
Reputation: 3658
I don't think this is easily done, as you'd have to find a way to ask windows for how it sorts it's files in that given folder. After that, you'd have to get the file attributes of each file in the folder and use that to sort in your code.
Upvotes: 0
Reputation: 39447
The files have no particular order in a folder. For example in Windows you
can sort by Name, or by Last Modification Date, or by Size, etc.
So not really sure your question is valid.
Upvotes: 3