java - 需要程序方面的帮助,FileInputStream 只读取文本文件的一行时遇到问题。 [作业帮助]

标签 java xml fileinputstream

所以我必须编写一个程序来测试 XML 文本文档是否正确嵌套。我想这实际上是我们这学期比较容易的作业之一,但我遇到的麻烦是逐行阅读文本文件。我已经为我们的教授提供给我们的 FileInputStream 实现了代码,并且从那里我一直遇到问题。基本上,我的代码验证文本文件的第一行,然后结束。我的 FileInputStream 一定是做错了什么,我只是不太确定是什么。感谢您的帮助。

// The XMLParser class prompts the user for a filename and reads an XML 
// document from the given text file. The program then reports that either the 
// document is valid based on XML rules or there was an error at a certain 
// line of input due to a violation of rules of XML tags.

import java.io.*;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class XMLParser {        
public static void main(String[] args) throws IOException{

    //creates a scanner to read the users input of the file name
    Scanner input = new Scanner(System.in);

    //creates an integer that will be incremented for every line read
    //to help with error reporting
    int currentLine = 1;

    //creates a stack that elements will be pushed on to
    //and popped off of, strings to represent opening and closing
    //tags of an xml document, and a string to represent the root 
    //tag (the first tag used)
    Stack<String> stack = new Stack<String>();
    String open = "<";
    String close = "</";
    String root = "";


    //prompts the user to input a file name then reads from
    // the file the user entered
    System.out.println("Enter the name of the file to be read: ");
    String fileName = input.next();
    FileInputStream fis = new FileInputStream(fileName);
    InputStreamReader inStream = new InputStreamReader(fis);
    BufferedReader stdin = new BufferedReader(inStream);
    String data = stdin.readLine();

    String nextDataValue;
    StringTokenizer tokenizer = new StringTokenizer(data);

    while (tokenizer.hasMoreTokens()) {
        nextDataValue = tokenizer.nextToken().trim();

        //if the xml document begins with anything other than
        //a root tag, an error is reported and the program ends
        if (root.equals("") && !nextDataValue.startsWith(open)) {
            System.out.println("PARSE ERROR Line 1, your XML document does not start with an opening tag and is therefore invalid\nProgram terminating normally...");
            break;
        }

        //pushes an "opening tag" onto the stack
        else if (nextDataValue.startsWith(open) && !nextDataValue.startsWith(close)) {
            stack.push(nextDataValue);

            //checks if there is already a root value, and if there
            //is not it adds one
            if (root.equals("")) {
                root = root + nextDataValue;
            }

        }

        //if a closing tag is found, this pops the stack and compares
        //the closing tag with an opening tag to see if it is properly
        //nested, if not an error statement is printed and the program
        //ends
        if (nextDataValue.startsWith(close)) {
            String compareClose = nextDataValue;
            String compareOpen = stack.pop();
            compareClose = compareClose.replace("/", "");
            if (!compareClose.equals(compareOpen)) {
                System.out.println("PARSE ERROR Line " + currentLine + ", you have improperly nested the tag: " + compareOpen);
                break;
            }
            //if the closing tag is the root tag and there is nothing
            //after this closing tag, the document is valid.
            if (compareOpen.equals(root) && !tokenizer.hasMoreTokens()) {
                System.out.println("Input XML document is valid");
            }
        }       


        currentLine++;
    }




}
}

tl;dr:为什么我的 FileInputStream 通过一行代码然后就停止了。我是在我的 while 循环中做错了什么还是我不知道的其他事情?

非常感谢任何帮助,谢谢!

最佳答案

您的问题在于这一行:String data = stdin.readLine();。你这样做的方式将指示程序只读取一行,验证它,然后退出。你需要做的是类似于这样的 while 循环:

String data = "";
while ( (data = stdin.readLine()) != null)
{
    //Read and validate the line you are reading
}

这将允许您在每次迭代时加载一行新文本。一旦遇到 EOFstdin.readLine() 应该返回 null,从而中断循环并停止程序执行。

关于java - 需要程序方面的帮助,FileInputStream 只读取文本文件的一行时遇到问题。 [作业帮助],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512249/

相关文章:

android - RecyclerView 内容落后而不是上面的 TextView

java - 使用 FileInputStream 和 FileOutputStream 作为缓冲区

java - 什么可能导致程序无法读取整个文件?

java - 获取 FileInputStream 使用的文件

java - 如何使用配置管理为同一类创建多个配置?

javascript - eBay Finding API - 为什么 findItemsAdvanced JSON 结果元素都是数组?

java - 我如何处理 Hibernate 中的身份与平等?

java - 计算胜/负、胜率和总胜率

java - 无法在 GAE 留言簿示例中将 JSP 设置为欢迎文件

JAVA方法调用方法