2012-03-20 10 views
0

誰もが複数の行を読み、その値を格納する方法を教えてもらえますか?ファイルのアップロードで複数の行を読む

例:撮影上のファイル、考えられる原因とアクションでfile.txtを

Probable Cause: The network operator has issued an alter attribute command for 
the specified LCONF assign. The old value and the new value are show 
Action Taken : The assign value is changed from the old value to the new 
value. Receipt of this message does not guarantee that the new attribute 
value was accepted by clients who use it. Additional messages may be. 

Probable Cause: The network operator has issued an info attribute command for 
the specified LCONF assign. The default value being used is displaye 
Action Taken : None. Informational use only. 

データベーステーブルの列です。そして、後で考えられる原因:それらは、考えられる原因列のためにデータベーステーブルに格納される値です。

どうすれば複数の行を読み込み、その値を保存できますか?私はAction Takenがラインに来るまで、考えられる原因の値を読み取る必要があります。私はBufferedReaderreadLine()メソッドを使用して、一度に1行を読み込みます。だから、どれくらいの線がそれらの間にあるかにかかわらず、考えられる原因から行動を直接読む方法を誰にでも教えてもらえます。

答えて

1

最も簡単な方法は、単にループで、各値のために、このような何かList<String>を保つために、おそらくです:あなたがペアを「原因」/「とられる動作を」持ってたら

private static final String ACTION_TAKEN_PREFIX = "Action Taken "; 

... 

String line; 
while ((line = reader.readLine()) != null) 
{ 
    if (line.startsWith(ACTION_TAKEN_PREFIX)) 
    { 
     actions.add(line.substring(ACTION_TAKEN_PREFIX)) 
     // Keep reading the rest of the actions 
     break; 
    } 
    causes.add(line); 
} 
// Now handle the fact that either we've reached the end of the file, or we're 
// reading the actions 

を、文字列のリストを単一の文字列に戻します。 「\ n」で結合し、データベースに挿入します。 (。GuavaJoinerクラスは、これが容易になります)

トリッキーなビットは、異常を扱っている:

  • あなたが原因で始まらない場合はどうなりますか?
  • 考えられる原因の後に別の原因が続く場合、または一連のアクションの後に別のアクションが続く場合はどうなりますか?
  • 恐らく原因はあるが動作のリストは読んだ後にファイルの最後に達した場合はどうなりますか?

は、私が今完全ソリューションを書き出すための時間を持っていないが、うまくいけば、上記のあなたが軌道に乗るのに役立ちます。

関連する問題