Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.io.File;
Import Javax.swing.JComboBox;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
public class FileUtils {
/**
- lists the names of all files or directories in the specified folder (directory)
- @param the folder (directory) specified by the Dir file type
- @return
- @throws illegalaccess Exception
/
public static String listdirectory (File dir) throws illegalaccessexception{
// Determines whether the file and directory associated with DIR exists
if (!dir.exists ()) {
///If it does not exist, throws an exception
throw new Illegalaccessexception ("directory" + dir + "does not exist.) ");
}
//Determine if DIR is associated with a directory
if (!dir.isdirectory ()) {
throw new illegalaccessexception (dir + "not Directory");
}
/ Gets the name of all files and folders under
- current directory (dir) by invoking the list () method with the passed-in file Object dir.
*/
string[] files = dir.list ();
String m = "";
for (String a:files) {
m=m+a+ "\ n";}
return m;
}
/**
- @param args
*/
public static void Main (string[] args) {
//TODO auto-generated method stub
JFrame frame=new JFrame ();
JPanel main_panel =new JPanel (New BorderLayout ());
Frame.setdefaultcloseoperation (jframe.exit_on_close);
String[] Itme = {". gif", ". bmp"};
JComboBox frm=new JComboBox (itme);
Frm.setenabled (TRUE);
Frm.seteditable (TRUE);
Frm.setmaximumrowcount (5);
Frm.setbounds (230,30,130,25);
Frame.setbounds (300,200,350,300);
Frame.setvisible (TRUE);
JTextArea main_text =new JTextArea ();
Main_text.setbackground (Color.White);
JScrollPane z=new JScrollPane ();
Z.setviewportview (Main_text);
Main_text.setenabled (FALSE);
Main_panel.add (Frm,borderlayout.north);
Main_panel.add (Z,borderlayout.center);
Frame.add (Main_panel);
try {
String str = fileutils.listdirectory (new File ("D:\FileUtils"));
Main_text.settext (str);
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Java Learning Notes (v)