亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將匹配后的所有內容存儲在字符串中的數組列表中

將匹配后的所有內容存儲在字符串中的數組列表中

ITMISS 2022-12-28 16:38:43
我有一個方法,它獲取一個文件,其中包含一個 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;
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

它不應該太難:


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))){

    ...

}


查看完整回答
反對 回復 2022-12-28
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號