Reputation: 113
HI, I have almost solved this but have now got stuck! What I need to do is look in a folder say..
String path = C://;
I then need a loop to see how many files are in that folder say 10. I need to look for files that start like
LAYER.EXE-******.pf
*****
can change depending of other things but thats not important what is, is making sure when it finds a file that starts LAYER.EXE- it flags it up. Below is what I have been working on, I would vert much like you're help and thank you in advance! :)
String path = "C://";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++){
if (listOfFiles[i].isFile()){
files = listOfFiles[i].getName();
System.out.println(files);
if (files == "LAYER.EXE.pf"){
System.out.println("found =================");
}
}
}
Upvotes: 4
Views: 8862
Reputation: 114817
If you need to filter filenames, then consider using a filename filter:
File[] files = directory.listFiles(new FilenameFilter() {
public boolean accept(File dir, String fileName) {
return fileName.startsWith("LAYER.EXE-") && fileName.endsWith(".pf");
}
});
This filter is based on your requirements and not on your current implementation.
The files
array now only contains File
objects whose the filenames are accepted by the filter, in other words, files that match the "pattern" LAYER.EXE-****.pf
Upvotes: 1
Reputation: 5114
You can use a FileFilter to obtain only the files that fit your name pattern. Like this:
String path = "C://";
File folder = new File(path);
File[] listOfFiles = folder.listFiles(new FileFilter(){
public boolean accept(File f){
return (f.startsWith("LAYER.EXE"));
}
});
And there you'll have an array with all the files you need, and only them. Next you can test if the array is void and if not handle the files.
Upvotes: 0
Reputation: 10215
You can use FileNameFilter
public void listFiles() {
File f = new File("C:/");
String[] allFiles = f.list(new MyFilter ());
for (String filez:allFiles ) {
System.out.println(filez);
}
}
class MyFilter implements FilenameFilter {
@Override
public boolean accept(final File dir, final String name) {
return name.startsWith("LAYER.EXE.pf");
}
}
Upvotes: 3
Reputation: 64
First of all, for string comparisons in Java, you need to do .equals()
instead of ==
as ==
will actually only evaluate to true if the strings are the same object, instead of comparing the values of the strings themselves.
So that would be
if(files.equal("LAYER.EXE.pf"))"
Secondly, you should use the .startsWith()
function to check if the first characters of a string matches another string
So, just do
if(files.startsWith("LAYER.EXE"))
and you should be fine
Upvotes: 0
Reputation: 6718
You want to compare the file names using one of the string comparisons, probably files.startsWith("LAYER.EXE.pf")
. equals()
(in this case) only returns true if the two objects are the same instance.
Upvotes: 0
Reputation: 2795
To check a String against another you should not use "==", but rather the equals method, or else you will compare the objects' references instead of the expressions. You can also use a better loop by making an iteration over your listOfFiles. Here is the corrected code :
String path = "C://";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (File checkedFile : listOfFiles){
if (checkedFile.isFile()){
files = checkedFile.getName();
System.out.println(files);
if (files.equals("LAYER.EXE.pf")){
System.out.println("found =================");
}
}
}
Upvotes: 0
Reputation: 181460
If you need files that start with LAYER.EXE
then you better change this:
if (files == "LAYER.EXE.pf")
Into:
if (files.startsWith("LAYER.EXE.pf"))
Please keep in mind that ==
operator on strings in Java won't behave as you might expect. Use equals
or equalsIgnoreCase
methods instead!
Upvotes: 0
Reputation: 43108
files == "LAYER.EXE.pf"
change to
"LAYER.EXE.pf".equals(files)
What you do is a reference comparison, and you need the equality. Read more here.
Telling more, this will give you only the files which name is equal to "LAYER.EXE.pf".
Try files.startsWith("LAYER.");
Upvotes: 6