Java searches for the specified (prefix, suffix or intermediate inclusion) file, java prefix

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.