Results 1 to 1 of 1
Thread: Help with Directories
- 03-27-2011, 12:09 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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
-
Managing directories with java
By bayan in forum New To JavaReplies: 3Last Post: 07-26-2010, 05:13 PM -
Directories
By KevMeistr in forum New To JavaReplies: 6Last Post: 06-11-2008, 04:03 PM -
JSP – getting list of directories
By Java Tip in forum Java TipReplies: 0Last Post: 01-31-2008, 12:51 PM -
getting paths and directories
By marco in forum Java AppletsReplies: 3Last Post: 11-25-2007, 07:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks