Q. Write a program to accept a string as command line argument and check whether it is a file or directory. If it is a directory, list the contents of the directory, count how many files the directory has and delete all files in that directory having extension .txt. (Ask the user if the files have to be deleted). If it is a file, display all information about the file (path, size, attributes etc).
Program:
import java.io.*;
class Slip11_1
{
public static void main(String a[])
{
String fname=a[0];
File f = new File(fname);
int num=0;
if(f.isDirectory())
{
System.out.println("Given file "+fname+"is directory :");
System.out.println("List of files are : ");
String s[] = f.list();
for(int i=0; i<s.length; i++)
{
File f1 = new File(fname, s[i]);
if(f1.isFile())
{
num++;
System.out.println(s[i]); //file name in directory
}
else System.out.println("\n"+s[i]+" is a sub directory");
}
System.out.println("\nNumber of files are: "+num);
}
else
{
if(f.exists())
{
System.out.println("\n"+fname+" is a File");
System.out.println("Details of "+fname+" are : ");
System.out.println("Path of file is "+f.getPath());
System.out.println("Absolute Path of file is "+f.getAbsolutePath());
System.out.println("Size of file is "+f.length());
}
else System.out.println(fname+" file is not present ");
}
}
}
Program:
import java.io.*;
class Slip11_1
{
public static void main(String a[])
{
String fname=a[0];
File f = new File(fname);
int num=0;
if(f.isDirectory())
{
System.out.println("Given file "+fname+"is directory :");
System.out.println("List of files are : ");
String s[] = f.list();
for(int i=0; i<s.length; i++)
{
File f1 = new File(fname, s[i]);
if(f1.isFile())
{
num++;
System.out.println(s[i]); //file name in directory
}
else System.out.println("\n"+s[i]+" is a sub directory");
}
System.out.println("\nNumber of files are: "+num);
}
else
{
if(f.exists())
{
System.out.println("\n"+fname+" is a File");
System.out.println("Details of "+fname+" are : ");
System.out.println("Path of file is "+f.getPath());
System.out.println("Absolute Path of file is "+f.getAbsolutePath());
System.out.println("Size of file is "+f.length());
}
else System.out.println(fname+" file is not present ");
}
}
}
3 Comments
This code is incomplete my fellow students. The files are not deleted in the directory having .txt extension
ReplyDeleteFrom what I know, currently many programs or applications are implemented in Java. Finally, when we use applications in the cloud, this programming language is definitely suitable for it. I am happy to use solutions provided from grapeup and I am very happy with their application.
ReplyDeleteSlip14_1: Write a program to accept a number from the user, if number is zero then throw user
ReplyDeletedefined exception “Number is 0” otherwise check whether no is prime or not (Use static keyword).
import java.util.Scanner;
import java.util.*;
class Zerono extends Exception
{}
class Prime
{
static int count=0;
public static void main(String args[])
{
int no,i,j;
Scanner sc=new Scanner(System.in);
try
{
System.out.println("enter no");
no=sc.nextInt();
if(no==0)
throw new Zerono();
if(no>0)
{
for(i=2;i<=no/2;i++)
{
if(no%i==0)
{
count++;
}
}
}
if(count==0)
System.out.println("No is Prime");
else
System.out.println("Not a Prime number");
}
catch(Zerono ob)
{
System.out.println("no can not be zero");
}
}
}