Results 1 to 1 of 1
  1. #1
    XDrew574X is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Searching directories for folders and .txt files

    I need some help with the coding that I have, please.

    I have to tell the console which directory I want to search in,
    I have to search the directory for .txt files,
    then I have to search the .txt for the words "I am a lawyer in * who has to find a lawyer overseas."

    I am so lost. This is the coding I have so far. Can anyone please guide me??
    Thanks in advance!


    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileFilter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.util.Scanner;

    /**
    * @author Melly
    */
    public class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException {
    String s;

    System.out.println("Starting Directory: ");
    Scanner kb = new Scanner(System.in);
    s = kb.nextLine();

    File f;
    f = new File(s); // Looks for files in a directory
    System.out.println(f);
    System.out.println(f.exists());
    System.out.println("Is directory? " + f.isDirectory());
    search(f);
    }

    private static void search(File f) throws FileNotFoundException, IOException {
    File[] allFiles = f.listFiles();
    for (File ff : allFiles) {
    if (ff.isDirectory()) {
    search(ff);
    } else {

    searchfortext(ff);
    }

    }
    }

    private static void searchfortext(File f) throws FileNotFoundException, IOException {
    DataInputStream kb = new DataInputStream(new DataInputStream(new FileInputStream(f.getPath())));
    String h = DataInputStream.readUTF(kb);
    System.out.println(h);

    }
    }
    /*
    FileFilter fileFilter = new FileFilter() {

    public boolean accept(File pathname) {
    return true; // or false
    }
    };


    }}
    Last edited by XDrew574X; 03-27-2011 at 01:06 AM.

Similar Threads

  1. Managing directories with java
    By bayan in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 05:13 PM
  2. Directories
    By KevMeistr in forum New To Java
    Replies: 6
    Last Post: 06-11-2008, 04:03 PM
  3. JSP getting list of directories
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-31-2008, 12:51 PM
  4. getting paths and directories
    By marco in forum Java Applets
    Replies: 3
    Last Post: 11-25-2007, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •