Archive

Archive for January 17, 2009

Java Program to read data from a file through inputfilestream and show it on the console. – Q34

January 17, 2009 Leave a comment

Q34: Java Program to read data from a file through inputfilestream and show it on the console.

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

class File_read{

	public static void main(String args[]) throws IOException{
		
		String f_name = JOptionPane.showInputDialog("Enter a File name: ");
		
		File f = new File(f_name);
		InputStream file = new FileInputStream(f);
		
		int fsize = file.available();
		
		System.out.println("Reading File : " + f.getName() + "\n");
		
		for(int i=0; i<fsize; i++)
			System.out.print((char)file.read());
	}
}

… from College notes (BCA/MCA assignments):


Advertisement

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