阅读文件时如何跳过某些文本区域?

我正在阅读一个.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循环,无法跳到下一行。


森林海
浏览 199回答 1
1回答

杨__羊羊

您永远不会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:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java