Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2005, 03:59 AM
Authorized User
 
Join Date: Aug 2005
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 %>
<tr>
<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 %>
</td></tr>
 
Old February 11th, 2007, 09:49 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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

Code:
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:

Code:
charlie@mogadon:~/filesort$ ls -1r
random
FileSort.java
FileSort.class
aaa
charlie@mogadon:~/filesort$ javac FileSort.java
charlie@mogadon:~/filesort$ java FileSort
Sorted list of files example
aaa
FileSort.class
FileSort.java
random
charlie@mogadon:~/filesort$
HTH,
Charlie

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





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





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