import java.io.*;
/*
*Program to list all the directories and files
under the given directory
*/
public class DirFiles{
public static void main(String[] args) {
File fil = new File(args[0]); //Input directory
from the command prompt
DirLevel dirlvl = new DirLevel(fil , 0);
checkIfDirectory(dirlvl );
}
//Recursive Method to get all the
files and directories under a given directory
static void checkIfDirectory(DirLevel fname){
for(int k=0;k<fname.level;k++){
System.out.print(" ");
}
if(fname.filename.isDirectory()) {
File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());
for(int i=0;i<filarray.length;i++){
DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}
else {
System.out.println(fname.filename.getName());
}
}
}
/*
*This calss is defined to determine the level
from the parent directory
*/
class DirLevel {
File filename ;
int level;
DirLevel(File flname , int lvl){
filename = flname;
level = lvl;
}
}
************************************************************************************
************************************************************************************
************************************************************************************
import java.io.*;
/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/
public class DirFilesCount {
public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);
checkIfDirectory(dirlvl );
}
//Recursive Method to get all
the directories and files and their line count, under a given directory
static void checkIfDirectory(DirLevel fname){
for(int k=0;k<fname.level;k++){
System.out.print(" ");
}
if(fname.filename.isDirectory()) {
File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());
for(int i=0;i<filarray.length;i++){
DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}
else {
BufferedReader buffreader=null ;
//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{
buffreader = new BufferedReader(new FileReader(fname.filename));
int linecount=0;
String line;
do {
line = buffreader.readLine();
if(line!=null) {
linecount++;
}
}while(line!=null);
System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");
}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){
try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}
}
}
}
}
************************************************************************************
************************************************************************************
************************************************************************************
import java.io.*;
/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/
public class DirFilesCount {
public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);
checkIfDirectory(dirlvl );
}
//Recursive Method to get all the directories and files and their line count,
under a given directory
static void checkIfDirectory(DirLevel fname){
for(int k=0;k<fname.level;k++){
System.out.print(" ");
}
if(fname.filename.isDirectory()) {
File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());
for(int i=0;i<filarray.length;i++){
DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}
else {
BufferedReader buffreader=null ;
//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{
buffreader = new BufferedReader(new FileReader(fname.filename));
int linecount=0;
String line;
do {
line = buffreader.readLine();
if(line!=null) {
linecount++;
}
}while(line!=null);
System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");
}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){
try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}
}
}
}
}
No comments:
Post a Comment