5
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by MakeMoneyOnline
0

Hi all,
I have built some code that prints out directory files and non-diectory files. I want to get some help in indenting my code further to achieve level 5 indentation since now its up to2. Any ideas please. Below is the code.

package com.jjpeople.Ronald.javafundamentals.FileIteration;


/**
*
*
*/
import java.io.*;
import java.io.File;
public class FileIterator
{
public void printContents(File folder,Boolean displayFiles,Boolean isSubDir)
{
String totalspace="";
String dirFilecount="";
String dirspace ="";
String filespace="";
DirUtils dirUtil=new DirUtils();
File[] directorys = folder.listFiles();
if (isSubDir)
{
dirspace ="   ";
}
for(int i=0;i<directorys.length;i++)
{


if (dirUtil.isDirectory(directorys))
{
totalspace=""+directorys.getTotalSpace();
dirFilecount=""+directorys.list().length;
System.out.println(dirspace+directorys.getName()+"(File count= "+ dirFilecount+" size="+totalspace+")");
//System.out.println("-------------------------------------");
printContents(directorys,displayFiles,true);
// dirspace=dirspace+ " ";
}
else
{
if (displayFiles==true)
{
System.out.println(dirspace + filespace+directorys.getName());
}
}
}
}
public static void main(String[]args) throws IOException
{
String x;
// TODO code application logic here
File rootFolder = new File("C:/javacode");
FileIterator fi = new FileIterator();
System.out.println("Do you want the directories and files to be written out or just directories? (Y or N) ");
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
x=in.readLine();


if ((x.equalsIgnoreCase("N")) || (x.equalsIgnoreCase("n")))
{
fi.printContents(rootFolder,false,false);
}
else if ((x.equalsIgnoreCase("Y")) || (x.equalsIgnoreCase("y")))
{
fi.printContents(rootFolder,true,false);
}
else
{
System.out.println("sorry you entered a wrong value");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


package com.jjpeople.Ronald.javafundamentals.FileIteration;


/**
*
* @author Ronald Ojino
*/
import java.io.File;
import java.io.*;
public class DirUtils implements FileFilter
{
public boolean accept(File file)
{
return file.isFile();
}
public static boolean isDirectory(File file)
{
return file.isDirectory();
}
public static boolean isNotDirectory(File file)
{
return  !file.isDirectory();
}
}



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


package com.jjpeople.Ronald.javafundamentals.FileIteration;


/**
*
* @author Ronald Ojino
*/
import java.io.File;
import java.io.*;
public class DirUtils implements FileFilter
{
public boolean accept(File file)
{
return file.isFile();
}
public static boolean isDirectory(File file)
{
return file.isDirectory();
}
public static boolean isNotDirectory(File file)
{
return  !file.isDirectory();
}
}

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.