0

i need to know if a specific string is contained in a list of files. The list of files is dynamic and i have to check a dynamic list of strings. This must be done in Java to work with the result (true or false). And MS Windows is also a requirement.

I thought about that problem i tried to use the unix way to do this:

find C:/temp | xargs grep import -sl

With GnuWin this works in cmd without any problems. So i tried to convert this into Java language. I read many articles about using Runtime class and ProcessBuilder class. But none of the tips are working. At last i tried the following two code snippets:

String binDir = "C:/develop/binaries/";

List<String> command = new ArrayList<String>();
command.add("cmd");
command.add("/c");
command.add(binDir+"find");
command.add("|");
command.add(binDir+"xargs");
command.add(binDir+"grep");
command.add("import");
command.add("-sl");

ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File("C:/temp"));
final Process proc = builder.start();

printToConsole(proc.getErrorStream());
printToConsole(proc.getInputStream());

int exitVal = proc.waitFor();

and

String binDir = "C:/develop/binaries/";
String strDir = "C:/temp/";

String[] command = {"cmd.exe ", "/C ", binDir + "find.exe " + strDir + " | " + binDir + "xargs.exe " + binDir + "grep.exe import -sl" };

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);

printToConsole(proc.getErrorStream());
printToConsole(proc.getInputStream());

int exitVal = proc.waitFor();

I also tried many other ways to concat the command, but either I get an error message (eg. file not found) or the process never comes back.

My questions: 1. Do you know a better way to do this job? 2. If not: Do you see any error in the code? 3. If not: Do you have another way i should try to run that command?

Thanks in advance.

ssc-seifert
  • 115
  • 1
  • 2
  • 8
  • I don't see your strDir in your first attempt. Is it a typo? – Guillaume Polet Apr 23 '12 at 14:40
  • No, the strDir from second is set by `builder.directory(new File("C:/temp"));` in the first one. – ssc-seifert Apr 23 '12 at 14:43
  • 1
    Aren't you confusing strDir and binDir? Anyway, personnally I would rather try to do this with Java code and if necessary a Java library/framework which does the same as your cmd. Although I like Unix commands power, the code you are writing is not very robust and not much portable either. – Guillaume Polet Apr 23 '12 at 14:46
  • Yes, i didn't understood your question right. Now i fixed my comment. :) I thought about reading all the files into java an searching "by hand". But the list of files can contain hundreds or thousands of files. Do you a framework for this? – ssc-seifert Apr 23 '12 at 14:48

2 Answers2

1

From the top of my head:

File yourDir = new File("c:/temp");
File [] files = yourDir.listFiles();
for(File f: files) {
     FileInputStream fis = new FileInputStream(f);
     try {
         BuffereReaded reader = new BufferedReader(new InputStreamReader(fis,"UTF-8")); // Choose correct encoding
         String s;
         while(((s=reader.readLine())!=null) {
             if (s.contains("import"))
              // Do something (add file to a list, for example). Possibly break out the loop
         }
     } finally {
           if (fis!=null)fis.close();
     }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
1

Java's support for subprocesses is pretty weak, especially on Windows. Avoid using that API if you don't really need to.

Instead, this SO question discusses how to replace the find for the recursive search, and the grep should be easy enough (especially with FileUtil.readLines(…) to help).

Community
  • 1
  • 1
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215