簡體   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