繁体   English   中英

在文件上传中读取多行

[英]reading multiple lines in file upload

谁能告诉我如何读取多行并存储其值。

例如: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.

在上面的文件中,可能原因和采取的措施是数据库表的列。 在“可能原因”之后:这些是要存储在数据库表中“可能原因”列的值,采取的措施也是如此。

那么我如何读取多行并存储它们的值呢? 我必须读取可能原因的值,直到该行附带采取的措施为止。 我正在使用BufferedReaderreadLine()方法来一次读取一行。 因此,任何人都可以告诉我如何从可能的原因直接读取所采取的措施,无论它们之间有多少条线。

最简单的方法可能是只保留一个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”连接,然后插入数据库。 Guava中Joiner类将使此操作更加容易。)

棘手的是处理异常:

  • 如果您不是从“可能的原因”入手,会发生什么?
  • 如果一个可能的原因之后是另一个原因,或者一组动作之后是另一种原因,将会发生什么?
  • 如果在读取可能的原因但没有操作列表后到达文件末尾,会发生什么情况?

我现在没有时间写出完整的解决方案,但是希望以上内容可以帮助您入门。

暂无
暂无

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

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