gpt4 book ai didi

java - 如何将标准输入转换为字符串?

转载 作者:行者123 更新时间:2023-11-30 09:50:20 29 4
gpt4 key购买 nike

我想知道如何将标准输入转换为字符串。例如,我有一个包含 n 个字母的 txt 文件 - 所有这些我都想读入一个字符串。

只是为了确保:标准输入是指您给程序一个 .txt 作为输入。

帮助将不胜感激!

最佳答案

I have a file and I want to store the data of it in a string.

这可以使用 commons-io 来完成:

String content = FileUtils.readFileToString(file);

( complete example )

要从标准输入读取字符串,您可以使用扫描器。如果需要整个文件,请使用 while (scanner.hasNextLine())

String firstLineFromStdin = new Scanner(System.in).readLine();

关于java - 如何将标准输入转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195739/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com