Java finds the beginning of a file name and extension

HI, I almost solved it, but now I'm stuck! What I need to do is look in the folder, say.

String path = C://; 

I need a loop to find out how many files in this folder says 10. I need to look for files that start as

 LAYER.EXE-******.pf 

***** may vary depending on other things, but it doesn’t matter what it is, checks to see if the file that launches LAYER.EXE- finds, it has its flag. Below I am working on this, I would be very similar to your help and in advance 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 ================="); } } } 
+4
source share
8 answers
 files == "LAYER.EXE.pf" 

change to

 "LAYER.EXE.pf".equals(files) 

What you do is compare links, and you need equality. More details here .

To tell you more, this will only give you files whose name is "LAYER.EXE.pf".

Try files.startsWith("LAYER.");

+6
source

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"); } } 
+3
source

If you need to filter file names, consider using a file name 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, not your current implementation.

Now the files array contains only File objects whose file names are accepted by the filter, in other words, files matching the "pattern" LAYER.EXE-****.pf

+1
source

If you need files starting with LAYER.EXE , then it is better to change this:

 if (files == "LAYER.EXE.pf") 

IN:

 if (files.startsWith("LAYER.EXE.pf")) 

Keep in mind that the == operator for strings in Java will not behave as you might expect. Use the equals or equalsIgnoreCase methods equalsIgnoreCase !

0
source

To check the string for another, you should not use "==", but rather the equals method, otherwise you will compare object references instead of expressions. You can also use the best loop by iterating over your lists. 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 ================="); } } } 
0
source

You want to compare file names using one of the string comparisons, possibly files.startsWith("LAYER.EXE.pf") . equals() (in this case) returns true only if both objects are the same instance.

0
source

First of all, to compare strings in Java, you need to do .equals() instead of == , since == will only evaluate 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 match another string

So just do if(files.startsWith("LAYER.EXE")) and you should be fine

0
source

You can use FileFilter to retrieve only files matching 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 will have an array with all the necessary files and only them. Then you can check if the array is invalid and not process the files.

0
source

Source: https://habr.com/ru/post/1337788/


All Articles