Package Liebiao;
Import Java.io.File;
Import Javax.swing.JFrame;
Import javax.swing.JList;
public class Liebiao extends jframe{
private static final File dir = null;
private static Object String;
/**
* @param args
* @throws illegalaccessexception
*/
public static string[] ListDirectory (File dir) throws illegalaccessexception{
To determine if the file object Dir exists, use the exists () method of the File class
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 ()) {
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[] FileNames = Dir.list ();
for (String s:filenames)
System.out.println (s);
Use the Listfiles () method to implement all file names that are listed in the directory only
file[] files = dir.listfiles ();
return fileNames;
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
JFrame frm = new JFrame ();
Frm.setbounds (500,200,300,500);
Frm.setvisible (TRUE);
try {
Liebiao.listdirectory (New File ("F:"));
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Package Liebiao;
Import javax.swing.*;
Import Javax.swing.border.CompoundBorder;
Import Javax.swing.border.EmptyBorder;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Scrollbar;
Import java.awt.event.*;
Import Java.io.File;
Import Java.io.File;
Import javax.swing.JList;
public class Fileutilsframe {
private static JLabel Infolabel;
private static JSlider slider;
private static JList list;
private static Listmodel FileName;
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
Final JFrame frm = new JFrame ("");
Frm.setdefaultcloseoperation (Jframe.exit_on_close);
jlist<string> JList = new Jlist<> (Liebiao.listdirectory (New File ("F:"));
Frm.add (jList);
JLabel Northlabel = new JLabel ("Files list");
Frm.add (Northlabel,borderlayout.north);
Frm.setbounds (400, 200, 200, 500);
Frm.setvisible (TRUE);
}
}
Java Fifth time job