Archive

Posts Tagged ‘Java IO Programs’

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

January 17, 2009 Leave a comment

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


Java Program to display file attributes like isfile(), exist() etc – Q32

January 16, 2009 Leave a comment

Q32: Java Program to display file attributes like isfile(), exist() etc

import java.io.*;

class file_attrib{

	static void p(String s){
		System.out.println(s);
	}

	public static void main(String args[]){
		File f1 = new File("fact_rec.java");
		p("File name: " + f1.getName());
		p("path: " + f1.getPath());
		p("Abs Path :" + f1.getAbsolutePath());
		p("Parent: "+ f1.getParent());
		p(f1.exists() ? "exists" : "dose not exist");
		p(f1.canWrite() ? "is writable" : "is not writable");
		p(f1.canRead() ? "is readable" : "is not readable");
		p("is " + (f1.isDirectory() ? "" : "not a directory"));
		p(f1.isFile() ? "is normal file" : "might be a named pipe");
		p(f1.isAbsolute() ? "is absolute" : "is not absolute");
		p("File last modified: " + f1.lastModified());
		p("File size: " + f1.length() + " Bytes");
		}
}

… from College notes (BCA/MCA assignments):