File Table HTML : Directory « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » DirectoryScreenshots 
File Table HTML
File Table HTML
      

/*
 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.Date;

/**
 * This class implements a simple directory browser using the HTML
 * display capabilities of the JEditorPane component.
 **/
public class FileTableHTML {
  public static void main(String[] argsthrows IOException {
    // Get the name of the directory to display
    String dirname = (args.length>0)?args[0]:System.getProperty("user.home");

    // Create something to display it in.
    final JEditorPane editor = new JEditorPane();
    editor.setEditable(false);               // we're browsing not editing
    editor.setContentType("text/html");      // must specify HTML text
    editor.setText(makeHTMLTable(dirname));  // specify the text to display
  
    // Set up the JEditorPane to handle clicks on hyperlinks
    editor.addHyperlinkListener(new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent e) {
  // Handle clicks; ignore mouseovers and other link-related events
  if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
    // Get the HREF of the link and display it.
    editor.setText(makeHTMLTable(e.getDescription()));
  }
      }
    });

    // Put the JEditorPane in a scrolling window and display it.
    JFrame frame = new JFrame("FileTableHTML");
    frame.getContentPane().add(new JScrollPane(editor));
    frame.setSize(650500);
    frame.setVisible(true);
  }

  // This method returns an HTML table representing the specified directory
  public static String makeHTMLTable(String dirname) {
    // Look up the contents of the directory
    File dir = new File(dirname);
    String[] entries = dir.list();

    // Set up an output stream we can print the table to.
    // This is easier than concatenating strings all the time.
    StringWriter sout = new StringWriter();
    PrintWriter out = new PrintWriter(sout);
    
    // Print the directory name as the page title
    out.println("<H1>" + dirname + "</H1>");

    // Print an "up" link, unless we're already at the root
    String parent = dir.getParent();
    if ((parent != null&& (parent.length() 0)) 
      out.println("<A HREF=\"" + parent + "\">Up to parent directory</A><P>");

    // Print out the table
    out.print("<TABLE BORDER=2 WIDTH=600><TR>");
    out.print("<TH>Name</TH><TH>Size</TH><TH>Modified</TH>");
    out.println("<TH>Readable?</TH><TH>Writable?</TH></TR>");
    for(int i=0; i < entries.length; i++) {
      File f = new File(dir, entries[i]);
      out.println("<TR><TD>" 
      (f.isDirectory() ?
         "<a href=\""+f+"\">" + entries[i"</a>" 
         entries[i]) +
      "</TD><TD>" + f.length() +
      "</TD><TD>" new Date(f.lastModified()) 
      "</TD><TD align=center>" (f.canRead()?"x":" "+
      "</TD><TD align=center>" (f.canWrite()?"x":" "+
      "</TD></TR>");
    }
    out.println("</TABLE>");
    out.close();

    // Get the string of HTML from the StringWriter and return it.
    return sout.toString();
  }
}


           
         
    
    
    
    
    
  
Related examples in the same category
1. Create directory
2. Create directory along with required nonexistent parent directories
3. Create directory tree (nested/cascade folders)
4. Create a directories recursively
5. Copying a Directory: Copies files under srcDir to dstDir, if dstDir does not exist, it will be created.
6. Delete a non-empty directory: Deletes all files and subdirectories under dir.
7. Listing the Files or Subdirectories in a Directory
8. Listing the File System Roots
9. The Directory Listing ApplicationThe Directory Listing Application
10. use list( ) to examine the contents of a directory:
11. Reading and Printing a Directory HierarchyReading and Printing a Directory Hierarchy
12. Display a file system in a JTree viewDisplay a file system in a JTree view
13. File Tree DemoFile Tree Demo
14. A standalone program that deletes a specified file or directory
15. Get Last modification time of a file or directory
16. Set last modified time of a file or directory
17. List contents of a directory
18. Determine if file or directory exists
19. Determine if File or Directory is hidden
20. Check if a directory is not empty
21. Get name of parent directory
22. Get name of specified file or directory
23. Get current directory
24. Mark file or directory Read Only
25. Rename file or directory
26. Traversing all files and directories under dir
27. Traversing only directories under dir
28. Traversing only files under dir
29. Creates and displays a window containing a list of files and sub-directories in a specified directory
30. Calculate directory size
31. Delete directory recursively
32. Determining If Two Filename Paths Refer to the Same File
33. Recursive directory deletion
34. Recursivly delete directory
35. Searches through the directory tree
36. Starts at the directory given and tests to see whether it is empty
37. Directory Walker
38. Utility methods for handling files and directories
39. Creates a new and empty directory in the default temp directory using the given prefix.
40. Count files in a directory (including files in all subdirectories)
41. Create a unique directory within a directory 'root'
42. Creates a new empty temporary directory.
43. Get Files Recurse
44. Recursively search a directory tree
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.