我正在閱讀一個(gè).txt文件,并希望在將結(jié)果放入StringBuilder.文本示例:以下 Bicycle 類是自行車的一種可能實(shí)現(xiàn):/* 自行車類 class Bicycle {int 節(jié)奏 = 0;整數(shù)速度 = 0; } */所以這就是我可以得出的結(jié)論: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(); }我得到了無(wú)限while循環(huán),無(wú)法跳到下一行。
1 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
您永遠(yuǎn)不會(huì)inputText在while循環(huán)中重置 的值,因此它永遠(yuǎn)不會(huì)以*/導(dǎo)致無(wú)限循環(huán)而結(jié)束。此外,在skip()遇到*/will 工作之前,您不需要使用該方法作為簡(jiǎn)單的閱讀行。嘗試將循環(huán)更改為:
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:
添加回答
舉報(bào)
0/150
提交
取消