gpt4 book ai didi

java - PrintWriter 处理多个 If else 语句

转载 作者:行者123 更新时间:2023-12-01 10:30:57 28 4
gpt4 key购买 nike

我有一个可以在控制台上正常打印的工作程序。但是,如果我想将其打印在输出文件中,我会遇到麻烦。它只执行 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 block 都会创建一个新文件,因此程序结束后您将看到的唯一输出是最终命令的输出,该命令位于最终 else block 中。

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

关于java - PrintWriter 处理多个 If else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097509/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com