import java.util.*;
import java.io.*;
import java.text.*;
public class Crawler//class decleration
{
private ArrayList<String> inputStrings = new ArrayList<String>();//creates a new instance of the arrray list object and protects it using the public keyword
private SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-mm-dd");
private File file;
private String archiveOutputDestination = "archive.txt";
private String copyOutputDestination = "copy.txt";
private String htmlOutputDestination = "report.htm";
private PrintStream archive;
private PrintStream copy;
private PrintStream html;
public Crawler()
{
try
{
archive = new PrintStream(archiveOutputDestination);
copy = new PrintStream(copyOutputDestination);
html = new PrintStream(htmlOutputDestination);
}
catch(FileNotFoundException e)
{
System.out.println("File cannot be openned or created please check that the file does not already exist");
}
}
public void main(String[] args)//method decleration
{
if(args.length <3)//if statement decleration
{
this.incorrectInput();//method execution
}
if(args.length>3)//if statement decleration
{
for(int i=0;i<args.length;i++)//
{
inputStrings.add(args[i]);//
}
this.dirMapper(inputStrings.get(0));//method execution
}
this.fileCorrect(file);//
}
public void incorrectInput()//method decleration
{
String dirName;//Creates a new String varriable called
String timeStamp;//Creates a new String varriable called
String extensions = "";//Creates a new String varriable called
Scanner directory = new Scanner(System.in);//creates a new instance of the scanner object
Scanner time = new Scanner(System.in);//creates a new instance of the scanner object
Scanner extension = new Scanner(System.in);//creates a new instance of the scanner object
Scanner inputscan = new Scanner(extensions);//creates a new instance of the scanner object
System.out.println("Invalid input in argument strings");//method which prints a line on the command line promting the user for
System.out.println("Please input a valid directory");//method which prints a line on the command line promting the user for
dirName = directory.nextLine();//assigns the next line of input from the command line to varriable
inputStrings.add(dirName);
System.out.println("Please input a valid time and date");//method which prints a line on the command line promting the user for
timeStamp = time.nextLine();//assigns the next line of input from the command line to varriable
inputStrings.add(timeStamp);
System.out.println("Please input valid file extensions in a continuous string use a space to seperate each one");//method which prints a line on the command line promting the user for
extensions = extension.nextLine();//assigns the next line of input from the command line to varriable
while(inputscan.hasNext())//
{
inputStrings.add(extensions);//
}
this.dirMapper(inputStrings.get(0));//
}
public void dirMapper(String dirName)//method decleration
{
try
{
dirName = inputStrings.get(0);
File file = new File(dirName);//
File[]files = file.listFiles();//
for(int i=0;i<files.length;i++)//
{
files[i].getName();//
if(files[i].isDirectory())//
{
this.dirMapper(dirName+"/"+files[i].getName());//
}
else//
{
this.fileExtensionFilter(file);//
}
}
}
catch (StackOverflowError e)
{
System.out.println("An error has ocurred");
System.err.println("A diificulty has been risen whilst trying to read the directory"+dirName);
}
}
public boolean fileExtensionFilter(File files)//method decleration
{
for (int i=2; i><inputStrings.size(); i++)
{
if(files.getName().substring(files.getName().length() - inputStrings.get(i).length()).equalsIgnoreCase(inputStrings.get(i)))
{
return true;
}
}
return false;
}
public boolean archiveFilter(File file)//method decleration
{
Date date = new Date();
if(file.lastModified() >< (date.getTime() - (100 * (24 * (60 * (60 * 100)))) ) )
{
return true;
}
return false;
}
public boolean hasBeenModified(File file)//method decleration
{
Date date = new Date();
try
{
simpledateformat.parse(inputStrings.get(1));
}
catch(ParseException e)
{
System.out.println("Invalid date input. You will now be redirected to re input the data");
this.incorrectInput();
}
if(file.lastModified() > (date.getTime()))
{
return true;
}
return false;
}
public void fileCorrect(File file)
{
int i =0;
ArrayList <File> toBeArchived = new ArrayList <File>();
ArrayList <File> toBeCopied = new ArrayList <File>();
if(this.fileExtensionFilter(file))
{
for(i=0;i<toBeArchived.size();i++)
{
System.out.println("toBeArchived"+file+i);
if(this.archiveFilter(file))
{
toBeArchived.add(i, file);
}
}
for(i=0;i<toBeCopied.size();i++)
{
System.out.println("toBeCopied"+file+i);
if(this.hasBeenModified(file))
{
toBeCopied.add(i, file);
}
}
}
this.archiveOutput(toBeArchived, archive);
this.copyOutput(toBeCopied, copy);
this.htmlOutput(toBeCopied, toBeArchived, html);
}
public void archiveOutput(ArrayList ><File> toBeArchived, PrintStream archive)//method decleration
{
int i;
archive.print("Files to be Archived");
archive.println();
archive.print("File");
archive.print("Path");
archive.print("Last Modified ");
archive.println();
for(i=0;i<toBeArchived.size();i++)
{
archive .print(toBeArchived.get(i).getName());
archive.print(toBeArchived.get(i).getPath());
archive.print(toBeArchived.get(i).lastModified());
archive.println("");
}
archive.close();
}
public void copyOutput(ArrayList ><File> toBeCopied, PrintStream copy)//method decleration
{
int i;
copy.print("Files to be Copied");
copy.println();
copy.print("File");
copy.print("Path");
copy.print("Last Modified ");
copy.println();
for(i=0;i<toBeCopied.size();i++)
{
copy.print(toBeCopied.get(i).getName());
copy.print(toBeCopied.get(i).getPath());
copy.print(toBeCopied.get(i).lastModified());
copy.println("");
}
copy.close();
}
public void htmlOutput(ArrayList ><File> toBeCopied, ArrayList <File> toBeArchived, PrintStream html)//method decleration
{
int i;
html.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.println("<html>");
html.println("<head>");
html.println("<title>Archive/Copy Report</title>");
html.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"report.css\" />");
html.println("</head>");
html.println("<body class=\"body\">");
html.println("Archive/Copy Report");
html.println("<div class=\"archive\">");
html.println("Files to be Archived");
html.println("<table width=\"315\" border=\"1\" class=\"table\">");
html.println("<tr>");
html.println("<td>Filename</td>");
html.println("<td>Path</td>");
html.println("<td>Last Modified</td>");
html.println("</tr>");
html.println("<tr>");
for(i=0;i<toBeArchived.size();i++)
{
html.println("><td>"+ toBeArchived.get(i).getName() + "</td>");
html.println("<td>"+ toBeArchived.get(i).getPath()+"</td>");
html.println("<td>"+ toBeArchived.get(i).lastModified() +"</td>");
}
html.println("</tr>");
html.println("</table>");
html.println("</div>");
html.println("Files to be Copied");
html.println("<div class\"copy\">");
html.println("<table width=\"315\" border=\"1\" class=\"table\">");
html.println("<tr>");
html.println("<td>Filename</td>");
html.println("<td>Path</td>");
html.println("<td>Last Modified</td>");
html.println("</tr>");
html.println("<tr>");
for(i=0;i<toBeCopied.size();i++)
{
html.println("><td>"+toBeCopied.get(i).getName()+"</td>");
html.println("<td>"+toBeCopied.get(i).getPath()+"</td>");
html.println("<td>"+toBeCopied.get(i).lastModified()+"</td>");
}
html.println("</tr>");
html.println("</table>");
html.println("</div>");
html.println("</body>");
html.println("</html>");
html.close();
}
}
Remove this line:
dirName = inputStrings.get(0);
from dirMapper( ) function.
Also one advice, replace this one:
if(files.getName().substring(files.getName().length() - inputStrings.get(i).length()).equalsIgnoreCase(inputStrings.get(i)))
with something like:
if( files.getName( ).endsWith( inputStrings.get(i) ) )
It looks hmm... shorter :)