Advertisements
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


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):


Advertisements
  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: