Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Friday, January 30, 2009

Source Code in Java to Display all the files present in your system

//FileCrawl.java

This is the source code to display all the files in your system, from hard drives, disk drives, usb...

The explanation for this code can be found here Explanation


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;


public class FileCrawl
{
static File fileList,tempFile[],fileRoots[];
static ArrayList files,dir;
static BufferedWriter bw;
static FileWriter fw;

public FileCrawl()throws Exception
{

fw=new FileWriter("D:\\output.txt",true);
fileRoots=File.listRoots();
}

public static void main(String[] args)
{
try
{
FileCrawl fileCrawl=new FileCrawl();
for(int i=0;i<fileRoots.length;i++)
{
fileCrawl.read(fileRoots[i].toString());
}

}
catch(Exception e)
{


}


}

public void read(String drive) throws Exception
{
files=new ArrayList();
dir=new ArrayList();
fileList=new File("C:\\");

dir.add(drive);

for(int i=0;i<dir.size();i++)
{
fileList=new File(dir.get(i).toString());



tempFile=fileList.listFiles();


if(tempFile!=null)
{
for(int j=0;j<tempFile.length;j++)
{
String temp=tempFile[j].toString();
if(tempFile[j].isDirectory()==true)
{
dir.add(temp);
}
else
{
files.add(temp);
}
}
}
else
{
continue;
}
}


System.out.println(files.size());
for(int i=0;i<files.size();i++)
{
fw.write(files.get(i).toString());
fw.write("\n");
}
}

}

0 comments: