gpt4 book ai didi

java - 将数组列表中匹配后的所有内容存储在字符串中

转载 作者:行者123 更新时间:2023-12-02 04:51:57 25 4
gpt4 key购买 nike

我有一个方法,它获取一个带有 txt 文件路径作为参数的文件。缓冲读取器将一些行读入数组列表。到目前为止一切顺利,但现在我需要将特定元素之后的每个元素存储在字符串中。

示例:我在这个数组列表中有一个元素,在我点击这个元素后,它是“=”,我想将该元素之后的每个元素存储到一个字符串中。

我尝试了循环和 if 语句,但找不到解决方案。

    //Just for debugging purpose at some point i want to store a temperature in here and return it to Main
int temp = 1;

List<String> sensor_Daten = new ArrayList<>();

try
{
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;

while ((line = reader.readLine()) != null)
{
sensor_Daten.add(line);
}
}
catch (IOException IOEx)
{

}

return temp;

最佳答案

应该不会太难:

BufferedReader reader =  new BufferedReader(new   FileReader(path));
String line;
boolean isWordFound = false;
while ((line = reader.readLine()) != null) {

// add the line in the list if the word was found
if (isWordFound()){
sensor_Daten.add(line);
}

// flag isWordFound to true when the match is done the first time
if (!isWordFound && line.matches(myRegex)){
isWordFound = true;
}
}

顺便说一句,当你应该关闭流时,你不会关闭它。try-with-resource 语句会为您完成此操作。所以你应该喜欢这种方式。
概括地说:

BufferedReader reader = ...;
try{
reader = new BufferedReader(new FileReader(path));
}
finally{
try{
reader.close();
}
catch(IOException e) {...}
}

应该只是:

try(BufferedReader reader = new BufferedReader(new FileReader(path))){
...
}

关于java - 将数组列表中匹配后的所有内容存储在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56432105/

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