Java 数据读取器跳过我的空新行 (\n)

发布于 2024-12-16 15:55:53 字数 1446 浏览 0 评论 0原文

好吧,我尝试了一切,但找不到答案。我的数据读取器在读取 txt 文件时跳过空的下一行。 它应该从 txt 文件中删除所有注释并按原样打印其余数据。我的读者确实删除了评论和内容。打印数据但跳过空的新行..

MyDataReader.java

public String readLine()
{
    String buf = new String();
String readStr = new String();

int end = 0;
int done = 0;

try
{
    // checks if line extraction is done and marker has non null value
    while (done != 1 && marker != null)
    {
    readStr = theReader.readLine(); // Reads the line from standard input

    if (readStr != null)
    {
        /* If the first character of line isnt marker */
        if (readStr.length() > 0)
        {
            if (!readStr.substring(0, 1).equalsIgnoreCase(marker))
        {
            end = readStr.indexOf(marker);   // checks if marker exists in the string or not
            if (end > 0)
            buf = readStr.substring(0, end);
            else
            buf = readStr;

            done = 1;   // String extraction is done
         }
        }
    }
    else
    {
            buf = null;
        done = 1;
    }
    }
}
// catches the exception
catch (Exception e)
{
    buf = null;
    System.out.println(e);
}

return buf;
}

TestMyDataReader.java

String myStr = new String();

  myStr = _mdr.readLine();

  while (myStr != null)
  {
      //System.out.println("Original String : " + myStr);
      System.out.println(myStr);
      myStr = _mdr.readLine();
  }

Okay, I tried everything but I can't find answer. My data reader skips empty next line while reading from a txt file.
It is supposed to strip all the comments from the txt file and print rest of the data as it is. My reader does strip the comments & prints the data but it skips the empty new lines..

MyDataReader.java

public String readLine()
{
    String buf = new String();
String readStr = new String();

int end = 0;
int done = 0;

try
{
    // checks if line extraction is done and marker has non null value
    while (done != 1 && marker != null)
    {
    readStr = theReader.readLine(); // Reads the line from standard input

    if (readStr != null)
    {
        /* If the first character of line isnt marker */
        if (readStr.length() > 0)
        {
            if (!readStr.substring(0, 1).equalsIgnoreCase(marker))
        {
            end = readStr.indexOf(marker);   // checks if marker exists in the string or not
            if (end > 0)
            buf = readStr.substring(0, end);
            else
            buf = readStr;

            done = 1;   // String extraction is done
         }
        }
    }
    else
    {
            buf = null;
        done = 1;
    }
    }
}
// catches the exception
catch (Exception e)
{
    buf = null;
    System.out.println(e);
}

return buf;
}

TestMyDataReader.java

String myStr = new String();

  myStr = _mdr.readLine();

  while (myStr != null)
  {
      //System.out.println("Original String : " + myStr);
      System.out.println(myStr);
      myStr = _mdr.readLine();
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2024-12-23 15:55:53

if (readStr.length() > 0)

这是跳过空行的代码行。

if (readStr.length() > 0)

That's the line of code that is skipping empty lines.

泪之魂 2024-12-23 15:55:53

这段代码中有很多问题,但您正在处理的主要问题是新行不包含在 readLine 结果中。因此,您的 if 语句不正确(该行实际上是空的

lots of issues in this code, but the main problem that you are dealing with is that new lines are not included in the readLine result. Thus, your if statement is not true (the line is in fact empty

☆獨立☆ 2024-12-23 15:55:53

您的阅读器不会在 readStr 中包含换行符,因此读取“\n”行将使 readStr 为“”,并且

readStr.length() > 0

将计算为 false,从而跳过该行。

Your reader won't include the newline characer in readStr, so reading in the line "\n" will make readStr be "", and

readStr.length() > 0

Will evaluate to false, thus skipping that line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文