我有一个文件,格式如下:
header line 1
header line 2
header line 3
line 1
line 2
line 3
...
我需要得到 Iterable<String>
在标题之后使用这些行( line 1
、 line 2
、 line 3
等)。如何在 Cactoos 的帮助下实现这一目标?标题由空行分隔。
我可以用这段代码跳过标题行:
new Skipped<>(
new SplitText(
new TextOf(this.path),
"\n"
),
4
);
现在如何映射 Iterable<Text>
将跳过的 header 添加到 Iterable<String>
?
我正在尝试用这段代码来做,但它不起作用:
new Mapped<>(
new FuncOf<>(
input -> input.asString()
),
new Skipped<>(
new SplitText(
new UncheckedText(
new TextOf(
this.path
)
),
"\n"
),
4
)
);
最佳答案
当你打电话时:
new FuncOf<>(
input -> input.asString()
)
您实际上是在调用 FuncOf<>(final Y result)
,这不是您想要的。
尝试提供一个 lambda 作为 Func
的实现反而。例如以下作品:
@Test
public void test() {
final String text =
"header line 1\n" +
"header line 2\n" +
"header line 3\n" +
"\n" +
"line 1\n" +
"line 2\n" +
"line 3";
MatcherAssert.assertThat(
new Mapped<>(
txt -> txt.asString(),
new Skipped<>(
new SplitText(
text,
"\n"
),
4
)
),
Matchers.contains("line 1", "line 2", "line 3")
);
}
关于java - 如何在 cactoos 的空白字符串之后从文件中检索字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51270619/