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[] args) throws 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(650, 500);
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(" " + dirname + " ");
// Print an "up" link, unless we|re already at the root
String parent = dir.getParent();
if ((parent != null) && (parent.length() > 0))
out.println("Up to parent directory ");
// Print out the table
out.print("");
out.print("");
out.println("");
for(int i=0; i < entries.length; i++) {
File f = new File(dir, entries[i]);
out.println("");
}
out.println("NameSizeModifiedReadable?Writable?" + (f.isDirectory() ?"" + entries[i] + "" : entries[i]) +" " + f.length() +" " + new Date(f.lastModified()) + " " + (f.canRead()?"x":" ") +" " + (f.canWrite()?"x":" ") +"");
out.close();
// Get the string of HTML from the StringWriter and return it.
return sout.toString();
}
}
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[] args) throws 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(650, 500);
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(" " + dirname + " ");
// Print an "up" link, unless we|re already at the root
String parent = dir.getParent();
if ((parent != null) && (parent.length() > 0))
out.println("Up to parent directory ");
// Print out the table
out.print("");
out.print("");
out.println("");
for(int i=0; i < entries.length; i++) {
File f = new File(dir, entries[i]);
out.println("");
}
out.println("NameSizeModifiedReadable?Writable?" + (f.isDirectory() ?"" + entries[i] + "" : entries[i]) +" " + f.length() +" " + new Date(f.lastModified()) + " " + (f.canRead()?"x":" ") +" " + (f.canWrite()?"x":" ") +"");
out.close();
// Get the string of HTML from the StringWriter and return it.
return sout.toString();
}
}
No comments:
Post a Comment