Missing line when reading using input stream reader

I have a sample EXE which prints below output.

EXEs Output:

1
2
3
4
5
Failed

equivalent code in java:

for (int i = 1; i <= 5; i++){
     System.out.println(i);
}
System.out.println("Failed");

When trying to initiate the EXE using java code and read the output some data goes missing.

Find the java for initiating the asset.

Java Code:

String[] commands = new String[] {"sample.exe" };
p = Runtime.getRuntime().exec(commands);
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while  ((line = br.readLine()) != null) {
    line = br.readLine()
    System.out.println(line);
    if(line.toLowerCase().contains("failed")){
           #Apply business Rule.
    }
}

Output:

1
3
5

From above output it is clear that we are missing data like 2,3,5,failed .

I think the asset gets completed before we read using InputStreamReader . Is there any way we can make the asset wait till we read using InputStreamReader and begin the next set of instruction or is there any other more better way to do this.

Edit1:

In my original code I am also reading the error stream in parallel please find the code.

ErrorStreamReader:

public void run () {
    try {

        InputStreamReader isr = new InputStreamReader (is);
        BufferedReader br = new BufferedReader (isr); 
        while (true) {
                String s = br.readLine ();
                System.out.println(s+"error Stream");
                if (s == null) break;
            }

    is.close ();    
    } catch (Exception ex) {
        System.out.println ("Problem reading stream " + name + "... :" + ex);
        ex.printStackTrace ();
    }
}

EXE executer java code:

String[] commands = new String[] {"sample.exe" };
p = Runtime.getRuntime().exec(commands);
Thread errorStream = new Thread(new ReadStream("stderr", 
    p.getErrorStream ()) # Reads error Stream In parallel
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while  ((line = br.readLine()) != null) {
    line = br.readLine()
    System.out.println(line);
    if(line.toLowerCase().contains("failed")){
           #Apply business Rule.
    }
}

It looks like the input stream reader is reading by skipping one line at a time.

EDIT2:

Since I was under the thought that InputStreamReader was missing even number lines I made a change in the EXE (a python script) to print from 1 to 6 and then failed.

Modified EXE Output:

1
2
3
4
5
6
Failed

Now the InputStreamReader output was

Java Output:

1
3
5
Failed

As I thought I am missing odd numbered lines. Could somebody please let me know the cause?.


Note:

The problem was due to reading the inputStream Twice sorry for the inconvenience caused. I am extremely sorry.


The problem was due to reading the inputStream twice

while  ((line = br.readLine()) != null) {
    line = br.readLine() # input stream read here twice
    System.out.println(line);
    if(line.toLowerCase().contains("failed")){
           #Apply business Rule.
    }
}

Hence the problem. To solve it I only read inputStream once.

while  ((line = br.readLine()) != null) {
    System.out.println(line);
    if(line.toLowerCase().contains("failed")){
           #Apply business Rule.
    }
}
链接地址: http://www.djcxy.com/p/93992.html

上一篇: 对话框的Android M权限问题“不要再询问”

下一篇: 使用输入流阅读器读取时缺少行