2012-03-01 78 views
1

我正在使用以下代碼將InputStream讀到StringREGEX:解析大字符串

public String convertStreamToString(InputStream is) { 
    try { 
     return new Scanner(is).useDelimiter("\\A").next(); 
    } catch (NoSuchElementException e) { 
     return ""; 
    } 
} 

但是,我得到一個非常大的String作爲輸出。我怎樣才能解析它得到Free blocksBlock count作爲兩個獨立字符串的值?

03-01 09:28:25.772: I/System.out(8849): tune2fs 1.41.10 (10-Feb-2009) 
03-01 09:28:25.772: I/System.out(8849): Filesystem volume name: <none> 
03-01 09:28:25.772: I/System.out(8849): Last mounted on:   <not available> 
03-01 09:28:25.772: I/System.out(8849): Filesystem UUID:   c6a5907b-71b3-4686-9ed1-8dd932c6359b 
03-01 09:28:25.772: I/System.out(8849): Filesystem magic number: 0xEF53 
03-01 09:28:25.772: I/System.out(8849): Filesystem revision #: 1 (dynamic) 
03-01 09:28:25.772: I/System.out(8849): Filesystem features:  has_journal ext_attr dir_index filetype sparse_super 
03-01 09:28:25.772: I/System.out(8849): Filesystem flags:   unsigned_directory_hash 
03-01 09:28:25.772: I/System.out(8849): Default mount options: (none) 
03-01 09:28:25.772: I/System.out(8849): Filesystem state:   clean 
03-01 09:28:25.772: I/System.out(8849): Errors behavior:   Continue 
03-01 09:28:25.772: I/System.out(8849): Filesystem OS type:  Linux 
03-01 09:28:25.772: I/System.out(8849): Inode count:    24480 
03-01 09:28:25.772: I/System.out(8849): Block count:    97656 
03-01 09:28:25.772: I/System.out(8849): Reserved block count:  4882 
03-01 09:28:25.772: I/System.out(8849): Free blocks:    90433 
03-01 09:28:25.772: I/System.out(8849): Free inodes:    24469 
03-01 09:28:25.772: I/System.out(8849): First block:    1 
03-01 09:28:25.772: I/System.out(8849): Block size:    1024 
03-01 09:28:25.772: I/System.out(8849): Fragment size:   1024 
03-01 09:28:25.772: I/System.out(8849): Blocks per group:   8192 
03-01 09:28:25.772: I/System.out(8849): Fragments per group:  8192 
03-01 09:28:25.772: I/System.out(8849): Inodes per group:   2040 
03-01 09:28:25.772: I/System.out(8849): Inode blocks per group: 255 
03-01 09:28:25.772: I/System.out(8849): Filesystem created:  Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Last mount time:   n/a 
03-01 09:28:25.772: I/System.out(8849): Last write time:   Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Mount count:    0 
03-01 09:28:25.772: I/System.out(8849): Maximum mount count:  35 
03-01 09:28:25.772: I/System.out(8849): Last checked:    Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Check interval:   15552000 (6 months) 
03-01 09:28:25.772: I/System.out(8849): Next check after:   Tue Aug 28 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Reserved blocks uid:  0 (user root) 
03-01 09:28:25.772: I/System.out(8849): Reserved blocks gid:  0 (group root) 
03-01 09:28:25.772: I/System.out(8849): First inode:    11 
03-01 09:28:25.772: I/System.out(8849): Inode size:   128 
03-01 09:28:25.772: I/System.out(8849): Journal inode:   8 
03-01 09:28:25.772: I/System.out(8849): Default directory hash: half_md4 
03-01 09:28:25.772: I/System.out(8849): Directory Hash Seed:  fb1c85fc-7de0-431e-be07-aa42bee66e7d 
03-01 09:28:25.772: I/System.out(8849): Journal backup:   inode blocks 
03-01 09:28:25.819: I/System.out(8849): length = 1512 
03-01 09:28:25.819: I/System.out(8849): 2 

回答

3

[編輯]很抱歉沒有意識到你與你的掃描儀在做什麼:

Scanner scanner = new Scanner(is).useDelimiter("\\n"); 
    scanner.findWithinHorizon(Pattern.compile("Block\\scount:\\s*(\\d+)"), 0); 
    String blockCount = scanner.match().group(1); 
    scanner.findWithinHorizon(Pattern.compile("Free\\sblocks:\\s*(\\d+)"), 0); 
    String freeBlocks = scanner.match().group(1); 

這將會給你免費塊和塊計數的只是值。例如,freeBlocks將等於「90433」,並且blockCount將等於「97656」。

+0

我得到'03-01 10:07:49.436:W/System.err(9432):java.lang.IllegalStateException:目前沒有成功匹配 '。 :( – 2012-03-01 04:38:56

+0

仍然返回null – 2012-03-01 05:40:33

+0

它現在匹配,但返回整行,問題與順序。BlockCount應該被放置在第一個。 – 2012-03-01 06:34:03

0
Scanner sc = new Scanner(is); 
while (sc.hasNextLine()) { 
    String line = sc.nextLine(); 
    // If line contains information 
     // Parse Line 
    // Else 
     // continue 
} 

我會留下試圖確定該行是否包含您要查找的信息的邏輯。