java - 如何在 cactoos 的空白字符串之后从文件中检索字符串?

标签 java oop cactoos

我有一个文件,格式如下:

header line 1
header line 2
header line 3

line 1
line 2
line 3
...

我需要得到 Iterable<String>在标题之后使用这些行( line 1line 2line 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/

相关文章:

java - 如何将 Map<T,List<L>> 映射中的值添加到 List<L>?

C++、oop、类列表(类类型)和创建它们的实例

java - 从 Iterable 计算 SumOfInts

java - 抛出 UncheckedIOException 而不是不同的预期异常

Java switch 语句多例

java - 如何解析这个输入?

java - 如何在没有互联网连接时使用 firebase 获取对手用户的在线/离线状态

c++ - 如何设计适合数百万分配的类?

java - 为什么 java "highest"父类(super class)称为 "Object"?