Results 1 to 2 of 2
  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
    }
    };


    }}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your post is the same as your previous post from 3/26: Help with Directories

    Probably the reason you didn't get help then is that you didn't ask a specific question but rather a very broad and general statement "I am so lost. This is the coding I have so far. Can anyone please guide me?", one that isn't readily answerable.

    If you want a better chance of getting help, I suggest you try to solve each problem one at a time in separate very small programs, programs that do nothing but try to solve the single problem at hand.

    For instance, first create a small program that tries to get information from the user to see which directory to search in, and then print the input out. Next create a small program that has the directory hard-coded in (to simplify it) and does nothing but tries to search a given directory for all .txt files, and then prints out the list. Last create another very small program that have to searches these text files for the words "I am a lawyer in * who has to find a lawyer overseas." When/if you get stuck at a specific step, please let us know, post any debug attempts too, and any error messages. The better your question, the more likely you'll get a helpful answer. Also, please read my signature link below on how to use code tags, for if your code is easier to read, more will likely read it and help you.

    Luck.

Similar Threads

  1. java searching from many files
    By manish250 in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 08:50 AM
  2. Save files in directories
    By Tota in forum New To Java
    Replies: 2
    Last Post: 09-02-2010, 03:49 PM
  3. List of files and directories.
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 01-02-2010, 05:06 PM
  4. Replies: 4
    Last Post: 10-10-2009, 01:26 AM
  5. Replies: 20
    Last Post: 12-17-2007, 04:20 PM

Tags for this Thread

Posting Permissions

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