Java
Slip12
Q.1) Write a java program to accept list of file names through command line and delete the files having extension “.txt”. Display the details of remaining files such as FileName and size. [Marks 30]
Solution
class Delete
{
public static void main(String args[]) throws Exception
{
String str[]=new String[100];
// Vector list=new Vector();
for(int i=0;i<args.length;i++)
{
File file=new File(args[i]);
if(file.isFile())
{
System.out.println(args[i]+"\t"+file.length());
}
else
{
System.out.println(args[i]+ " is not a file");
}
}
File dirs = new File(new File(".").getAbsolutePath());
str = dirs.list(); // to delete all .html files of current directory
//to use vector
/* for(int i=0;i<args.length;i++)
{
list.addElement(args[i]);
}
list.copyInto(str);
*/
for(int i=0;i<str.length;i++)
{
if(str[i].endsWith("txt"))
{
File tmp=new File(str[i]);
if(tmp.delete())
{
System.out.println("File Deleted");
}
}
}
}
}
Tags:
java