我正在閱讀一個.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:
添加回答
舉報
0/150
提交
取消