简体   繁体   English

多个If Else语句上的PrintWriter

[英]PrintWriter on Multiple If Else Statement

I have a working program which prints fine on the console. 我有一个可以在控制台上正常打印的工作程序。 However I am having trouble if i want to print it in an output file. 但是,如果要在输出文件中打印它,我会遇到麻烦。 It only executes the Else statement. 它仅执行Else语句。 It will not go into the If and Else statement. 它不会进入If and Else语句中。

 public void processFile()
  {
    final String IN = "IN"; 
    final String OUT = "OUT";
    final String CLOSE = "CLOSE";
    PrintWriter fileOut;

    final String FILE_NAME_IN  = "Events.txt";
    final String FILE_NAME_OUT = "SystemLog.txt";
    BufferedReader fileIn;
    String inputLine;

    try
    {
      // print out initial state 
      fileOut = new PrintWriter(new FileWriter(FILE_NAME_OUT));
      System.out.println("---------");
      fileOut.println("---------");
      System.out.println("INCOMING PROCESS");
      fileOut.println("INCOMING PROCESS");
      System.out.println(list.toString()); 
      fileOut.println(list.toString());
      System.out.println("OUTGOING PROCESS");
      fileOut.println("OUTGOING PROCESS");
      System.out.println(queue.toString()); 
      fileOut.println(queue.toString()); 
      fileOut.close();
    }
    catch(IOException ioe)
    {
      ioe.printStackTrace();
    }


    try
    {
      // instantiate reader and grab the first line
      fileIn = new BufferedReader(new FileReader(FILE_NAME_IN));
      inputLine = fileIn.readLine(); 

      // While we still have events to process... 

      while(inputLine != null)
      {            
        String [] tokens = inputLine.split(" "); // read and tokenize line from text file 

        if(tokens[0].equals(IN)) 
        {                      

          try
          {
              fileOut = new PrintWriter(new FileWriter(FILE_NAME_OUT));
            System.out.println("\n---------");
            fileOut.println("\n---------");
            System.out.println(tokens[0] + " " + tokens[1]); 
            fileOut.println(tokens[0] + " " + tokens[1]); 
            System.out.println("---------");
            fileOut.println("---------");
            System.out.println("\n\n");
            fileOut.println("\n\n");

            // print status     
            System.out.println("INCOMING PROCESS");
            fileOut.println("INCOMING PROCESS");
            System.out.println(list.toString()); 
            fileOut.println(list.toString());
            System.out.println("OUTGOING PROCESS");
            fileOut.println("OUTGOING PROCESS");
            System.out.println(queue.toString()); 
            fileOut.println(queue.toString()); 
            fileOut.close();
          }
          catch(IOException ioe)
          {
            ioe.printStackTrace();
          }

        }
        else if(tokens[0].equals(OUT))
        {      
          try
          {
             fileOut = new PrintWriter(new FileWriter(FILE_NAME_OUT));
            System.out.println("\n---------");
            fileOut.println("\n---------");
            System.out.println(tokens[0] + " " + tokens[1]); 
            fileOut.println(tokens[0] + " " + tokens[1]); 
            System.out.println("---------");
            fileOut.println("---------");
            System.out.println("\n\n");
            fileOut.println("\n\n");

            // print status     
            System.out.println("INCOMING PROCESS");
            fileOut.println("INCOMING PROCESS");
            System.out.println(list.toString()); 
            fileOut.println(list.toString());
            System.out.println("OUTGOING PROCESS");
            fileOut.println("OUTGOING PROCESS");
            System.out.println(queue.toString()); 
            fileOut.println(queue.toString()); 
            fileOut.close();
          }
          catch(IOException ioe)
          {
            ioe.printStackTrace();
          }
        }
        else //CLOSE
        {
          try
          {
              fileOut = new PrintWriter(new FileWriter(FILE_NAME_OUT));
            System.out.println("\n\n---------");
            fileOut.println("\n\n---------");
            System.out.println(tokens[0]); 
            fileOut.println(tokens[0]); 
            System.out.println("---------");
            fileOut.println("---------");
            System.out.println("\n\n");
            fileOut.println("\n\n"); 

            // print status     
            System.out.println("INCOMING PROCESS");
            fileOut.println("INCOMING PROCESS");
            System.out.println(list.toString()); 
            fileOut.println(list.toString());
            System.out.println("OUTGOING PROCESS");
            fileOut.println("OUTGOING PROCESS");
            System.out.println(queue.toString()); 
            fileOut.println(queue.toString()); 
            fileOut.close();
          }
          catch(IOException ioe)
          {
            ioe.printStackTrace();
          }
        }

        inputLine = fileIn.readLine(); // grab the next line    
      }
      fileIn.close();
    }

    catch(IOException ioe)
    {
      System.out.println("Error: Could not read file.");
      ioe.printStackTrace(); 
    }
  }

You are mistaken. 你误会了。 The problem is that every if/else block creates a new file, so the only output you will see after the program has ended is the output from the final command, which is in the final else block. 问题是每个if / else块都会创建一个新文件,因此程序结束后,您将看到的唯一输出是final命令的输出,该命令位于final else块中。

There's no need to open and close the file in every if or else block. 无需在每个if or else块中打开和关闭文件。 Open it once at the beginning, write to it whenever you need to, and close it at the end. 首先将其打开,在需要时对其进行写入,最后将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM