gpt4 book ai didi

java - 将从文件读取的数据捕获到字符串流Java中

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:50 24 4
gpt4 key购买 nike

我有 C++ 背景,所以请善待我的 n00bish 查询...

我想从输入文件读取数据并将其存储在字符串流中。我可以使用字符串流在 C++ 中以简单的方式完成此任务。我在尝试用 Java 做同样的事情时有点迷失了。

以下是我开发的粗略代码/方式,我将逐行读取的数据存储在字符串数组中。我需要使用字符串流将数据捕获到(而不是使用字符串数组).. 有帮助吗?

    char dataCharArray[] = new char[2];
int marker=0;
String inputLine;
String temp_to_write_data[] = new String[100];

// Now, read from output_x into stringstream

FileInputStream fstream = new FileInputStream("output_" + dataCharArray[0]);

// Convert our input stream to a BufferedReader
BufferedReader in = new BufferedReader (new InputStreamReader(fstream));

// Continue to read lines while there are still some left to read
while ((inputLine = in.readLine()) != null )
{
// Print file line to screen
// System.out.println (inputLine);
temp_to_write_data[marker] = inputLine;
marker++;
}

编辑:

我认为我真正想要的是 StringBuffer。我需要从文件中读取数据(可能读取到 StringBuffer 中)并将所有数据写入/传输回另一个文件。

最佳答案

在 Java 中,应始终优先考虑从图书馆购买代码:

http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html

简而言之,您需要的是:

FileUtils.readFileToString(File file)

关于java - 将从文件读取的数据捕获到字符串流Java中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520897/

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