繁体   English   中英

如何获取InputStream以行作为String数组?

[英]How do I get my InputStream to give me lines as a String array?

我有过程,并得到一个InputStream像:

myInputStream = getProcess().getInputStream();

我想以某种方式能够调用这些方法

String[] getLines(int start, int stop); // Will return a String array of those lines    
String[] getLines(int start); // Will return a String array of lines from the line number to the end    
String[] getLines(); //Returns the entire output as an array seperated by the end of the line.   

另外,我将同时运行多个进程,因此将整个日志始终存储在内存中并不是我认为的最佳方法。

Scanner类在这里可能会有所帮助,因为它可以从InputStream读取行。 请注意,我提供了一个简单的测试用例来验证内容。

我同意@LouisWasserman的评论,即InputStream并不真正适合此操作,但以下操作可能有效。

public class LogReaderTest {
    public static class LogReader {
        private final Scanner scanner;

        public LogReader(final InputStream stream) {
            scanner = new Scanner(stream);
        }

        public String[] getLines(int start, int stop) {
            fastForward(start);
            int stopVal = stop <= start ? Integer.MAX_VALUE : stop - start;

            final List<String> rows = new ArrayList<>();
            for (int i = 0; i < stopVal; i++) {
                if (scanner.hasNextLine()) {
                    rows.add(scanner.nextLine());
                } else {
                    break;
                }
            }

            return rows.toArray(new String[rows.size()]);
        }

        public String[] getLines(int start) {
            return getLines(start, -1);
        }

        public String[] getLines() {
            return getLines(0);
        }

        private void fastForward(final int lines) {
            for (int i = 0; i < lines; i++) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                } else {
                    throw new IllegalStateException("Unable to scan line");
                }
            }
        }
    }

    @Test
    public void testLogReader() throws IOException {
        final String[] lines = {"first", "second", "third", "fourth"};
        final String stringWithNewLines = Arrays.stream(lines).collect(Collectors.joining("\n"));

        Assert.assertEquals(
            4, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines().length);
        Assert.assertEquals(
            2, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines(2).length);
        Assert.assertEquals(
            3, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines(1, 4).length);
    }
}

使用BufferedReader逐行读取InputStream。 试试这个代码,或者您可以实现自己的逻辑。

String[] getLines(){
    return getLines(0,0);
}
String[] getLines(int start){
    return getLines(start,0);
}
String[] getLines(int start,int stop){
    ArrayList<String> list = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new 
        InputStreamReader(myInputStream));
    String line;
    Integer count=1;
    try {
            while ((line = reader.readLine()) != null) {
                if(count>=start){
                    if(stop==0 || count<=stop)
                        list.add(line); 
                }
                count++;
            }
         reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }       
   return list.toArray(new String[list.size()]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM