adsense


Showing posts with label JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY. Show all posts
Showing posts with label JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY. Show all posts

Wednesday, 28 October 2015

JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY PAGE 120

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY





Java Examples - Search a file in a directory


Problem Description:
How to search for a file in a directory?
Solution:
Following example shows how to search for a particular file 
in a directory by making a Filefiter. Following example 
displays all the files having file names starting with 'b'.
import java.io.*;

class Main {
   public static void main(String[] args) {
      File dir = new File("C:");
      FilenameFilter filter = new FilenameFilter() {
         public boolean accept
         (File dir, String name) {
            return name.startsWith("b");
        }
      };
      String[] children = dir.list(filter);
      if (children == null) {
         System.out.println("Either dir does not 
         exist or is not a directory");
      } 
      else {
         for (int i=0; i7lt; children.length; i++) {
            String filename = children[i];
            System.out.println(filename);
         }
      } 
   }
}
Result:
The above code sample will produce the following result.
build
build.xml






JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY PAGE 113

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES SEARCH A FILE IN A DIRECTORY





Java Examples - Search a file in a directory


Problem Description:
How to search for a file in a directory?
Solution:
Following example shows how to search for a particular file in a directory by making a Filefiter. Following example displays all the files having file names starting with 'b'.
import java.io.*;

class Main {
   public static void main(String[] args) {
      File dir = new File("C:");
      FilenameFilter filter = new FilenameFilter() {
         public boolean accept
         (File dir, String name) {
            return name.startsWith("b");
        }
      };
      String[] children = dir.list(filter);
      if (children == null) {
         System.out.println("Either dir does not 
         exist or is not a directory");
      } 
      else {
         for (int i=0; i7lt; children.length; i++) {
            String filename = children[i];
            System.out.println(filename);
         }
      } 
   }
}
Result:
The above code sample will produce the following result.
build
build.xml