Java searches for the specified (prefix, suffix or intermediate inclusion) file, java prefix
1 import java. io. file; 2 import java. io. fileNotFoundException; 3 4 public class SearchFile {5 6 public static void main (String [] args) throws FileNotFoundException {7 File files = new File ("F :/"); // create a File object pointing to the root directory of drive F 8 String [] names = files. list (); // obtain all files and paths in the root directory of drive F, and return 9 for (String s: names) using a String Array {// traverse the String array 10 boolean a = s. startsWith ("ja"); // returns true if the prefix of the File name contains ja, and false11 boolean B = (new File (files. getAbsolutePath () + s )). isFile (); // determines whether the content pointed to by the string of this loop is a file. If yes, true is returned. otherwise, false12 boolean c = s. contains ("va"); // if the file name contains "va", true is returned. Otherwise, false13 if (a & B) {// modify the conditions as needed. 14 System. out. println (s); // print out the qualified file 15} 16} 17} 18 19}
Row 3: Details
If
Boolean B = (new File (files. getAbsolutePath () + s). isFile ();
Change
Boolean B = (new File (s). isFile ();
The system will always return false. Because no path is specified, the system will find the file name pointed to by s in the project directory, but the file is not in the project directory, so it will always judge that the file does not exist.