Showing posts with label execute linux commands in java. Show all posts
Showing posts with label execute linux commands in java. Show all posts

Monday, February 15, 2010

Executing linux commands from Java

package sunels;

import java.io.*;

public class LinuxCommandExecutor {
public static void main (String args[]) {
try {
ProcessBuilder pb = new ProcessBuilder ("/bin/grep", "phoneNumber", "/space/subscribersDBdump.txt");

Process proc = pb.start ();

BufferedInputStream in = new BufferedInputStream (proc.getInputStream ());
FileOutputStream fout = new FileOutputStream ("/space/phoneNumbersOnly.txt");

byte[] buffer = new byte[2048];

while (true) {
int nRead = in.read (buffer);

if (nRead == -1)
break;

fout.write (buffer, 0, nRead);
System.out.print (".");
}

fout.close ();
proc.waitFor ();

}
catch (Exception e) {
e.printStackTrace ();
}
}
}