如何在 cactoos 的空白字符串之后从文件中检索字符串?
How to retrieve strings from file after the blank string with cactoos?
我有一个文件,格式如下:
header line 1
header line 2
header line 3
line 1
line 2
line 3
...
我需要在 header 之后使用这些行(line 1
、line 2
、line 3
等)获取 Iterable<String>
。如何在 Cactoos 的帮助下实现这一目标? header 由空行分隔。
我能够使用此代码跳过 header 行:
new Skipped<>(
new SplitText(
new TextOf(this.path),
"\n"
),
4
);
现在如何将跳过的 header 的 Iterable<Text>
映射到 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")
);
}
我有一个文件,格式如下:
header line 1
header line 2
header line 3
line 1
line 2
line 3
...
我需要在 header 之后使用这些行(line 1
、line 2
、line 3
等)获取 Iterable<String>
。如何在 Cactoos 的帮助下实现这一目标? header 由空行分隔。
我能够使用此代码跳过 header 行:
new Skipped<>(
new SplitText(
new TextOf(this.path),
"\n"
),
4
);
现在如何将跳过的 header 的 Iterable<Text>
映射到 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")
);
}