我正在阅读一个.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 回答
![?](http://img1.sycdn.imooc.com/545850200001359c02200220-100-100.jpg)
杨__羊羊
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
提交
取消