0

In my program I am executing a shell script that writes output in a file. I want to display that file contents line by line. These two operations must work simultaneously.

Here is my code:

public static void myfun(String abc) throws IOException, InterruptedException {
    String customer = "xyz";
    nmapoutfile=runMT.instdir+"/var/nmapout."+customer;
    String nmapstatusfile = runMT.instdir+"/logs/nmapout."+customer+".log";
    String nmapdonefile = runMT.instdir+"/logs/nmapout."+customer+".done";

    String nmapcommand=runMT.instdir+"/bin/doscan.sh "+nmapoutfile+" "+IPRange+" "+nmapstatusfile+" "+nmapdonefile;

    System.out.println(nmapcommand);
    Runtime r = Runtime.getRuntime();
    Process pr = r.exec(nmapcommand);
    try {
        BufferedReader inbr = new BufferedReader (new FileReader(new File(nmapstatusfile)));        
    } 
    catch(Exception e){
         e.printStackTrace();
    }
    while (inbr.readLine()!=null){
        System.out.println("Line is .. " + inbr.readLine());
    }
    pr.waitFor();   
}

How do I handle concurrent read and write operation??

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Vishwas
  • 6,967
  • 5
  • 42
  • 69

1 Answers1

0

I would suggest you have a look at random access files (see http://docs.oracle.com/javase/tutorial/essential/io/rafs.html). This API is a little more complicated to work with since you'll be reading bytes, not lines, but it should give you what you need.

If you don't want to deal with NIO you can use http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html instead.

Akshat Singhal
  • 1,801
  • 19
  • 20