What wrong with my class?

F

Funan

After written this simple class, I was told it is way too flawed. May
it be my improper use of ArrayList? Please give your suggestions,
thanks in advance.


package filesearch;

import java.util.ArrayList;
import java.util.List;
import java.io.File;

public class FileNameSearch implements FileSearch
{
public List<String> searchFor(String fileName, String path)
{
List<File> files = getDirectoryContent(path);
List<String> matches = new ArrayList<String>();
for (File file : files)
{
if ((file.getName()).equals(fileName))
{
matches.add(file.getAbsolutePath());
}
}
return matches;
}

public List<File> getDirectoryContent(String path)
{
File directory = getDirectory(path);
if (!directory.exists())
{
return new ArrayList<File>();
}
return getFiles(directory);
}

public List<File> getFiles(File directory)
{
File[] files = getFilesInDirectory(directory);
ArrayList<File> result = new ArrayList<File>();
for (File file : files)
{
if (file.isFile())
{
result.add(file);
}
else
{
result.addAll(getFiles(file));
}
}
return result;
}

protected File getDirectory(String path)
{
return new File(path);
}

protected File[] getFilesInDirectory(File directory)
{
return directory.listFiles();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top