我有一个可以在控制台上打印的工作程序。但是,如果我想在输出文件中打印它,我会遇到麻烦。它只执行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();
}
}
答案 0 :(得分:1)
你错了。问题是每个if / else块都会创建一个新文件,因此在程序结束后您将看到的唯一输出是最终命令的输出,该输出位于最终的else
块中。
无需在每个if或else块中打开和关闭文件。在开头打开一次,随时写入,最后关闭它。