Fifth time Assignment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.