Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.ScrollPane;
Import Java.io.File;
Import Javax.swing.JComboBox;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import javax.swing.JList;
Import Javax.swing.JScrollPane;
Import Javax.swing.ListSelectionModel;
public class File {
Private static final file FileUtils = null;
/**
Lists the names of all files and subdirectories under the specified directory @throws illegalaccessexception
*/
public static void ListDirectory (File dir) throws
illegalaccessexception{// determines if the file object dir exists, using the file class's exists () method
if (!dir.exists ()) {
throws an exception if it does not exist
throw new Illegalaccessexception (" directory " + dir + " does not exist "); }
To determine if dir is a directory, use the isdirectory () method of the File class
if (!dir.isdirectory ()) {
if dir is not a directory, throw an exception
throw new IllegalArgumentException (dir + " not directory "); }
obtain the files and subdirectories under dir of the specified directory and output (1.list (); 2.listFiles ())
use the list () method to implement
string[] FileName = Dir.list ();
for (String s:filename) {
System.out.println (dir+ "/" +s);
}
System.out.println ();
use the listfiles () method to implement all file names that are listed in the directory only
file[] files = dir.listfiles ();
for (File file:files) {
if it is a file, the output
if (File.isfile ()) {
System.out.println (file);
}
}
JFrame a =new JFrame (" file directory ");
A.setdefaultcloseoperation (Jframe.exit_on_close);
A.setlayout (New BorderLayout ());
JLabel Northlabel = new JLabel ("Files list");
A.add (Northlabel,borderlayout.north);
JList list = new JList (files);
List.setforeground (New Color (140,200,220));
List.setbackground (Color.White);
List.setselectionbackground (New Color (100,80,150));
List.setselectionforeground (New Color (140,200,220));
JScrollPane sp = new JScrollPane (list);
A.add (Sp,borderlayout.center);
A.setbounds (400,300,400,300);
A.setvisible (TRUE);
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
Fileutils.listdirectory (New File ("D:\"));
} catch (Illegalaccessexception d) {
TODO auto-generated Catch block
D.printstacktrace ();
}
}
}
Fifth time Assignment