多个 If Else 语句上的 PrintWriter

PrintWriter on Multiple If Else Statement

我有一个可以在控制台上正常打印的工作程序。但是,如果我想在输出文件中打印它,我会遇到麻烦。它只执行 Else 语句。它不会进入 If 和 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(); 
    }
  }

你错了。问题是每个 if/else 块都会创建一个新文件,因此程序结束后您将看到的唯一输出是最终命令的输出,它位于最后的 else 块中。

无需在每个 if 或 else 块中打开和关闭文件。开头打开一次,需要的时候写入,最后关闭。