answersLogoWhite

0


Best Answer

The program below will search all non-directory files in the starting directory. If you want to search all subdirectories, you'll have to modify it to recurse a bit.

Note that this will also only do plain text searches of files. It will most likely not find your string in a MS Word .doc file.

Also note that this will not find multiple-line search strings, since it reads and checks one line of the file at a time.

{

final String dirName; // path of directory you want to search

final String str = ""; // string to search for

final File dir = new File(dirName);

for (File f : dir.listFiles()) {

if (!f.isDirectory()) {

System.out.println(f + ": " + fileContains(f, str));

}

}

}

// returns true iff f contains str

private static final boolean fileContains(final File f, final String str) throws IOException {

final BufferedReader in = new BufferedReader(new FileReader(f));

String currentLine;

while ((currentLine = in.readLine()) != null) {

if (currentLine.contains(str)) {

in.close();return true;

}

}

in.close();

return false;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

/**

* @param roots directories to start searching from

* @param regex regular expression used to find good matches

* @return a Collection of all File objects in each directory and

* subdirectory of roots whose filename matches regex.

*/

public static final Collection<File> getFiles(final String[] roots, final String regex) {

// Note: This method does very little error checking. It's left up

// to the user to provide valid input.

// storage of good File matches

final ArrayList<File> fileList = new ArrayList<File>();

// don't continue unless roots contains some data

if (roots == null roots.length > 0) {

// current working Stack of files yet-to-be-searched

final Stack<File> fileStack = new Stack<File>();

// add all root directories to file stack

for (String r : roots) {

fileStack.push(new File(r));

}

// start processing our file stack

File[] files;

while (!fileStack.empty()) {

// add all subdirectories of the current file to files

files = fileStack.pop().listFiles();

// if files is null, then one of our roots was not a directory

if (files != null) {

// search through files

for (int i = 0; i < files.length; ++i) {

// add any matches to our good file listing

if (files[i].getName().matches(regex)) {

fileList.add(files[i]);

}

// add any subdirectories to the file stack

if (files[i].isDirectory()) {

fileStack.push(files[i]);

}

}

}

}

}

return fileList;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you search the given word in all files from the directory in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is a java directory?

collection of files and directory


Any blogs for java?

You can search any blogs directory (e.g. technorati) for blogs in a certain area, in this case about Java.


How do you download java directory?

download java 7 it works by downloading the updated version of java directory


How do you find out how to use Java?

You can search online tutorials and if you read java files, you can learn by seeing things and figuring out how things work.


Is it possible to view and or edit the coding in java ME?

Yes and no. If you download and install the Java ME JDK, you will be given the option to install the source files for all Java classes in ME. This means that you can view the source by navigating to the directory you installed it to.However, you should not directly edit the classes found in the source directory, since the changes will not be reflected on any other machines. What you should do is make a subclass of whichever classes you want to edit and make changes from there. This will ensure that your code will always work.


Header files in Java programming?

Java does not require header files like C/C++.


How do you compile your java code?

save it into a .java file. then open command prompt and navigate to your java bin directory. then type CD then your java bin directory. then type this . javac ProgName.java


Where is the java directory?

Nowhere, Just Windows-r and type "JAVA"


What kind of file contain java source code?

'.java' files contain java source code. One can access these files on windows by using 'notepad'.


What are the Names of header files in java?

Java doesn't use header files.


What is java c?

Compiling Java ProgramsThe javac command is used to invoke Java's compiler and compile a Java source file.A typical invocation of the javac command would look like below:javac [options] [source files]Both the [options] and the [source files] are optional parts of the command, and both allow multiple entries. The following are both legal javac commands:javac -helpjavac ClassName.java OneMoreClassName.javaThe first invocation doesn't compile any files, but prints a summary of valid options. The second invocation passes the compiler two .java files to compile (ClassName.java and OneMoreClassName.java). Whenever you specify multiple options and/or files they should be separated by spaces.This command would create the .class files that would be require to run the java progam.Compiling with -dBy default, the compiler puts a .class file in the same directory as the .java source file. This is fine for very small projects, but once you're working on a project of any size at all, you'll want to keep your .java files separated from your .class files. The -d option lets you tell the compiler in which directory to put the .class files it generates (d is for destination).Lets take a look at two example commands:javac -d classes source/MyTestClass.javajavac -d ../classes com/scjp/test/MyTestClass.javaExample 1 - Compile file named "MyTestClass.java" that is present inside the "source" sub-directory of the current directory and put the .class file in the "classes" sub-directory of the current directoryExample 2 - Compile the file named "MyTestClass.java" that is present in the following directory hierarchy "com/scjp/test/" from the current directory and put the .class file in the folder "classes" that is present one level above the current directoryOne thing you must know about the -d option is that if the destination directory you specify doesn't exist, you'll get a compiler error. If, in the previous example, the classes directory did NOT exist, the compiler would say something like:java:5: error while writing MyTestClass: classes/ MyTestClass.class (No such file or directory)


What kind of files contain java souce code?

Those files have the extension .java