Import Java.awt.Color;
Import Java.io.File;
Import Javax.swing.JFrame;
Import javax.swing.JList;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
public class FileUtils {
/**
* Lists all files in the specified folder (directory) or the name of the directory
* @param dir file type indicates the specified folder (directory)
* @return
* @throws illegalaccessexception
*/
public static string[] ListDirectory (File dir) throws illegalaccessexception{
Determine if the file or directory to which dir is associated exists
if (!dir.exists ()) {
Throws an exception if it does not exist
throw new Illegalaccessexception ("directory" +dir+ "does not exist");
}
Determine if dir is associated with a directory
if (!dir.isdirectory ()) {
throw new IllegalArgumentException (dir+ "is not a directory");
}
Call the list () method with the file object passed in to get the names of all the files and folders under the current directory (dir) (excluding directories)
String[] Files =dir.list ();
for (String s:files)
System.out.println (dir+ "\ \" +s);
file[] FileNames = Dir.listfiles ();
for (File f:filenames) {
Determines whether the file object is a document, is the file output name
if (F.isfile ()) {
System.out.println (f);
return files;
}
/**
* @param args
* @throws illegalaccessexception
*/
public static void Main (string[] args) throws Illegalaccessexception {
TODO auto-generated Method Stub
Final JFrame frm = new JFrame ("");
Frm.setdefaultcloseoperation (Jframe.exit_on_close);
JList JList = new JList (Fileutils.listdirectory (New File ("C:\\users\\pc\\desktop\\java job 5\\soundplayer"));
Frm.add (jList);
Jlist.setbackground (Color.Black);
Jlist.setforeground (Color.Blue);
JMenuBar mbar = new JMenuBar ();
Frm.setjmenubar (mbar);
JMenu m1 = new JMenu ("Files");
JMenu m2 = new JMenu ("list");
Mbar.add (M1);
Mbar.add (m2);
Frm.setbounds (400, 200, 200, 300);
Frm.setvisible (TRUE);
try {
Fileutils.listdirectory (New File ("C:\\users\\pc\\desktop\\java job 5\\soundplayer"));
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}}
Java Fifth time job