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

Saturday, January 31, 2009

Source Code in Java to search for a File in your System -- Extension to the Earlier FileCrawl.java

//FileCrawl.java

The Explanation for the code can be found here : EXPLANATION

The Previous Source Code Version can be found here : SOURCE CODE Ver 1 and Explanation for that can be found here : Explanation



import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class FileCrawl implements ActionListener
{
static File fileList,tempFile[],fileRoots[];
static ArrayList dir;
static BufferedWriter bw;


JLabel searchLabel,rootLabel;
JTextField searchText;
JButton searchButton;
JTextArea resultArea;
JScrollPane resultPane;
JPanel searchPanel;
JFrame mainFrame;
JComboBox searchOption;
Pattern pattern;
Matcher matcher;

public FileCrawl()throws Exception
{


fileRoots=File.listRoots();

searchLabel=new JLabel("File Name: ");
searchLabel.setLabelFor(searchText);

rootLabel=new JLabel("Drives: ");
rootLabel.setLabelFor(searchOption);

searchText=new JTextField(30);
searchButton=new JButton("Search");
searchButton.addActionListener(this);

searchOption=new JComboBox(fileRoots);


searchPanel=new JPanel();
searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

searchPanel.add(searchLabel);
searchPanel.add(searchText);
searchPanel.add(rootLabel);
searchPanel.add(searchOption);
searchPanel.add(searchButton);


resultArea=new JTextArea(30,30);
resultArea.setEditable(false);


resultPane=new JScrollPane(resultArea);


mainFrame=new JFrame();
mainFrame.setTitle("File Crawl");
mainFrame.setLayout(new BorderLayout());

mainFrame.add(searchPanel,BorderLayout.NORTH);
mainFrame.add(resultPane,BorderLayout.CENTER);

mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setAlwaysOnTop(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public static void main(String[] args) throws Exception
{
FileCrawl fileCrawl=new FileCrawl();
}

public void read(String drive) throws Exception
{

dir=new ArrayList();
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
{

pattern= Pattern.compile(searchText.getText(),Pattern.CASE_INSENSITIVE);

matcher=pattern.matcher(temp);

if(matcher.find()==true)
{
resultArea.append(temp);
resultArea.append("\n");
}

}
}
}
else
{
continue;
}
}



}


public void actionPerformed(ActionEvent ae)
{
try
{
this.read(searchOption.getSelectedItem().toString());

}
catch(Exception e)
{

}
}

}

0 comments: