Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 21st, 2005, 03:59 AM
Authorized User
Join Date: Aug 2005
Location: Mumbai, Maharashtra, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to abhisheksud Send a message via Yahoo to abhisheksud
Default Sort the files in folder

I am making a webpage which shows all the file in a folder and make links to the file. However i want to know how i can sort the view of files according to file name or date created of by size(any one). I am using the for loop statement for displaying all the files.what modification should i do so that all the files are shown alphabetically in acending order...Also the file size is shown in bytes as a default. How can i make these appear in mb or kb to decimal digits....Pl help me out... the code which i am using is given below

for each file in files %>
<td style="width: 164px"><br />
<a href="patches/<%=file.name%>"><%=file.name%></a></td>
<td style="width: 196px" align=center><br /><%=file.size %> Kb</td>
<td style="width: 230px" align=center><br /><%=file.DateLastModified %>
  #2 (permalink)  
Old February 11th, 2007, 09:49 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Here's an example of how to do it by name. Date is trickier cf. http://forum.java.sun.com/thread.jsp...sageID=1850175

import java.util.*;   
import java.io.File;

public class FileSort {
  public static void main (String argv[]) {
    System.out.println("Sorted list of files example");
    File cwd = new File(".");        // current working directory
    String[] files = cwd.list();     // array of Strings of filenames
    Arrays.sort(files, String.CASE_INSENSITIVE_ORDER); // sorts them
    for (int i=0;i<files.length;i++) {
      System.out.println(files[i]);  // prints them

Gives me:

charlie@mogadon:~/filesort$ ls -1r
charlie@mogadon:~/filesort$ javac FileSort.java
charlie@mogadon:~/filesort$ java FileSort
Sorted list of files example

Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving files from one folder to another sachtaridis C# 1 November 23rd, 2007 04:33 AM
How to Zip files within a folder Udi C# 2 January 25th, 2007 12:45 AM
Files need to move different folder surendran PHP How-To 2 June 20th, 2006 10:27 PM
return files in folder Dj Kat Classic ASP Basics 2 February 9th, 2006 04:23 AM
FILES in FOLDER luma SQL Server DTS 0 June 9th, 2005 01:53 AM

All times are GMT -4. The time now is 06:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.