I am a begineer at java and wrote the below problem for postfix evaluataion. Although the program works fine for the console IO. It has problems when using files. I am trying to get the answers of the program in a file output00.txt, but the file is empty.
Here is the code. I have left out methods which are irrevanlant to this issue.
public static void main(String[] args) throws Exception {
FileReader input = new FileReader("input00.txt");
BufferedReader in = new BufferedReader(input) ;
FileWriter output = new FileWriter("output00.txt");
BufferedWriter out = new BufferedWriter(output);
while (true) {
String lineReader = in.readLine();
if (lineReader==null) {
break;
}
Stack<String> m_Stack = new Stack<String>();
m_Stack.addAll(Arrays.asList(lineReader.trim().split("[ \t]+")));
if (m_Stack.peek().equals("")){
continue;
}
try {
double finVal = evaluateRPN(m_Stack);
if (!m_Stack.empty()) {
throw new Exception();
}
out.write(finVal + "\n");
}
catch (Exception e) {System.out.println("error");}
}
}
File input00.txt contains
8 9 +
7 6 -
7 7 * 9 -
You need to call
out.flush()to flush what you’ve written to the file, or callout.close()which should automatically flush whatever’s been buffered. Actually, you should callclose()anyways because it’s good practice, if there’s some way for you to exit the while() loop.