作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能告诉我如何读取多行并存储它们的值。
例如: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.
在上面的文件中,可能的原因和采取的操作是数据库表的列。在“可能原因”之后:这些是要存储在数据库表中的“可能原因”列的值,所采取的操作也是如此。
那么我如何读取多行并存储它们的值?我必须读取可能原因的值,直到该行出现“已采取的操作”。我使用 BufferedReader 和 readLine() 方法一次读取一行。那么谁能告诉我如何直接从可能的原因读取采取的行动,无论它们之间有多少行。
最佳答案
最简单的方法可能就是保留 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
类将使这变得更容易。)
棘手的一点是处理异常:
我现在没有时间写出完整的解决方案,但希望以上内容能帮助您继续前进。
关于java - 读取文件上传中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782341/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!