Home > Java > Java Program to print all the directories, files within those directories and files in a particular directory – Q33

Java Program to print all the directories, files within those directories and files in a particular directory – Q33

January 17, 2009 Leave a comment Go to comments

Q33: Java Program to print all the directories, files within those directories and files in a particular directory.

import java.io.File;
import javax.swing.*;

class DirList{

	public static void main(String args[]){
		String dirname = JOptionPane.showInputDialog("Enter a directory: ");
		File f1 = new File(dirname);
		
		if(f1.isDirectory()){
			System.out.println("Directory of " + dirname);
			String s[] = f1.list();
			
			for(int i=0; i<s.length; i++){
				File f = new File(dirname + "/" + s[i]);
				if(f.isDirectory()){
					System.out.println(s[i] + " is a Directory");
				}
				else{
					System.out.println(s[i] + " is a File");
				}
			}
		}
		else{
			System.out.println(dirname + "is not a directory");
		}
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: