简体   繁体   中英

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. It will not go into the If and Else statement.

 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.

There's no need to open and close the file in every if or else block. Open it once at the beginning, write to it whenever you need to, and close it at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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