gpt4 book ai didi

java - Java中简单的逐行读取

转载 作者:行者123 更新时间:2023-12-01 13:51:29 24 4
gpt4 key购买 nike

我输入了以下格式:

12 : 13 24 1 2
2 : 1 4
9 : 0 1 82
...

我想将每一行作为 StringStream 读取,然后逐段读取每个 StringStream。

For example, line 1 would be stored as a StringStream "12 : 13 24 1 2", and then I could just read it in piece by piece ("12", ":", "13", "24", "1", "2") (something like cin >> stingstream in C++).

在 Java 中执行此操作的最佳方法是什么?

最佳答案

您尝试做的事情称为标记化:)

// pretty standard open file with buffered reader.
File file = new File("inputFileName");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedFileReader = new BufferedReader(fileReader);

// The string that will hold the content of each line.
String line;

// Read each line and store string content in line.
while((line=bufferedFileReader.readLine())!=null)
{
// Create a tokenizer to split the string into tokens.
StringTokenizer tokenizedLine= new StringTokenizer(line);

// For each token in the current line so something
while (while (tokenizedLine.hasMoreTokens())
{
System.out.println(tokenizedLine.nextToken());
}
}

关于java - Java中简单的逐行读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920894/

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