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

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

閱讀文件時如何跳過某些文本區域?

閱讀文件時如何跳過某些文本區域?

森林海 2021-06-28 12:57:25
我正在閱讀一個.txt文件,并希望在將結果放入StringBuilder.文本示例:以下 Bicycle 類是自行車的一種可能實現:/* 自行車類 class Bicycle {int 節奏 = 0;整數速度 = 0; } */所以這就是我可以得出的結論:public class Main {public static BufferedReader in;public static StringBuilder stringBuilder = new StringBuilder();public static void main(String[] args) {    String input = "input_text.txt";    try {        in = new BufferedReader(new FileReader(input));    } catch (FileNotFoundException e) {        e.printStackTrace();    }    String inputText;    try {        while ((inputText = in.readLine()) != null) {            if (inputText.startsWith("/*")) {// The problem is there:                while (!inputText.endsWith("*/")) {                    int lengthLine = inputText.length();                    in.skip((long)lengthLine);                }            }                stringBuilder.append(inputText);        }    } catch (IOException e) {        e.printStackTrace();    }我得到了無限while循環,無法跳到下一行。
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

您永遠不會inputText在while循環中重置 的值,因此它永遠不會以*/導致無限循環而結束。此外,在skip()遇到*/will 工作之前,您不需要使用該方法作為簡單的閱讀行。嘗試將循環更改為:


 while (!inputText.endsWith("*/")) {       

        String temp = in.readLine();

        if(temp == null) {break;}

        inputText = temp;                                                           

 }

輸出:(打印StringBuilder)


The following Bicycle class is one possible implementation of a bicycle:


查看完整回答
反對 回復 2021-07-07
  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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