简体   繁体   中英

FileWriter not writing to output file

I can run this code just printing to the console and everything prints fine, and then when I change all the System.out.println's to outputStream.write ("some string") nothing writes to the output file at all. It is just blank. The first file is the main file. Then my other classes are MyStack and PostfixError. I'll include them below this one. The code takes in input from an input file entered in the command line and then outputs it to a file also specified by the command line. The input file is supposed to be in Postfix format such as ABC+- or AB+C. If it is not in that format it will produce an error. If the input is in that format the code will take that input and write the instructions that would be needed to evaluate the postfix expression if we were using a computer with only one register.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;

public class PostfixEvaluation{
    public static void main(String[] args) throws PostfixError, IOException{
        FileReader inputStream = null;
        FileReader inputStream2 = null;
        FileWriter outputStream = null;
        BufferedReader str = null;
        int c, j=1,letter;
        int stringSize = 100;
        String message, letterA, letterB;



        try {
            inputStream = new FileReader(args[0]); // input file
            inputStream2 = new FileReader(args[0]);
            outputStream = new FileWriter(args[1]); // output file
            str = new BufferedReader(inputStream);
            String nextLine; //= str.readLine();

            while ((nextLine = str.readLine()) != null) {
                outputStream.write("");
                outputStream.write("Current expression being evaluated is " + nextLine);
                MyStack letterStack = new MyStack(stringSize);
                try {

                    while ((c = inputStream2.read()) != 10){
                            //letterStack = EvaluateInstructions(c, letterStack,j); 
                        letter = c;
                        if (letter == 65){ // use an or to account for lower case letters
                            letterStack.push("A");
                        }else if (letter == 66) {
                            letterStack.push("B");
                        }else if (letter == 67) {
                            letterStack.push("C");
                        }else if (letter == 68) {
                            letterStack.push("D");
                        }else if (letter == 69) {
                            letterStack.push("E");
                        }else if (letter == 70) {
                            letterStack.push("F");
                        }else if (letter == 71) {
                            letterStack.push("G");
                        }else if (letter == 72) {
                            letterStack.push("H");
                        }else if (letter == 73) {
                            letterStack.push("I");
                        }else if (letter == 74) {
                            letterStack.push("J");
                        }else if (letter == 75) {
                            letterStack.push("K");
                        }else if (letter == 76) {
                            letterStack.push("L");
                        }else if (letter == 77) {
                            letterStack.push("M");
                        }else if (letter == 78) {
                            letterStack.push("N");
                        }else if (letter == 79) {
                            letterStack.push("O");
                        }else if (letter == 80) {
                            letterStack.push("P");
                        }else if (letter == 81) {
                            letterStack.push("Q");
                        }else if (letter == 82) {
                            letterStack.push("R");
                        }else if ( letter == 83) { 
                            letterStack.push("S");
                        }else if (letter == 84) {
                            letterStack.push("T");
                        }else if (letter == 85) {
                            letterStack.push("U");
                        }else if (letter == 86) {
                            letterStack.push("V");
                        }else if (letter == 87) {
                            letterStack.push("W");
                        }else if (letter == 88){ 
                            letterStack.push("X");
                        }else if (letter == 89) {
                            letterStack.push("Y");
                        }else if (letter == 90) {   
                            letterStack.push("Z");

                        }else if (letter == 42){                //letter == '*'
                            if (letterStack.isEmpty()|letterStack.length() <= 1) {
                                j=1;
                                throw new PostfixError("There are more operators than operands; This is not a valid expression.");
                            }else {
                                letterA = letterStack.pop();
                                letterB = letterStack.pop();
                                outputStream.write("LD   " + letterB);
                                outputStream.write("ML   " + letterA);
                                outputStream.write("ST   TEMP"+j);
                                letterStack.push("TEMP"+j);
                                //throw new PrintToOutput("hello");
                                //j++;
                            }
                        }else if (letter == 47) {               //letter == '/'
                            if (letterStack.isEmpty()|letterStack.length() <= 1) {
                                j=1;
                                throw new PostfixError("There are more operators than operands; This is not a valid expression.");

                            }else {
                                letterA = letterStack.pop();
                                letterB = letterStack.pop();
                                outputStream.write("LD   " + letterB);
                                outputStream.write("DV   " + letterA);
                                outputStream.write("ST   TEMP"+j);
                                letterStack.push("TEMP"+j);
                                //j++;
                            }
                        }else if (letter == 43) {               //letter == '+'
                            if (letterStack.isEmpty()|letterStack.length() <= 1) {
                                j=1;
                                throw new PostfixError("There are more operators than operands; This is not a valid expression.");

                            }else {
                                letterA = letterStack.pop();
                                letterB = letterStack.pop();
                                outputStream.write("LD   " + letterB);
                                outputStream.write("AD   " + letterA);
                                outputStream.write("ST   TEMP"+j);
                                letterStack.push("TEMP"+j);
                                //j++;
                            }
                        }else if (letter == 45) {               //letter == '-'
                            if (letterStack.isEmpty()|letterStack.length() <= 1) {
                                j=1;
                                throw new PostfixError("There are more operators than operands. This is not a valid expression.");

                            }else {
                                letterA = letterStack.pop();
                                letterB = letterStack.pop();
                                outputStream.write("LD   " + letterB);
                                outputStream.write("SB   " + letterA);
                                outputStream.write("ST   TEMP"+j);
                                letterStack.push("TEMP"+j);
                                //j++;
                            }
                        }else if (letter == 13) {
                            //do nothing
                        }else if (letter == -1) {
                            outputStream.write("");
                            outputStream.write("Success! File is finished being read.");
                            System.exit(0);
                        }else {
                            j=1;
                            //need to empty stack
                            throw new PostfixError( "This input has an incorrect character or combination of characters.");
                        }

                        if (c == 47 | c == 42 | c == 45 |c == 43) {
                            j++;
                        }else if (c == 13) {
                            j=1;
                        }
                    }


                }catch (PostfixError e) {
                    outputStream.write(e.getMessage());

                    //read input stream until it equals 10
                    do {
                        c=inputStream2.read();
                    }while (c !=10);

            }
            }
        } finally {
            if (inputStream != null) inputStream.close();
            if (inputStream2 !=null) inputStream2.close();
            if (outputStream != null) outputStream.close();
            str.close();
        }
    }
}

MyStack Class

public class MyStack {

    private String[] theStack;
    private int size;
    private int top;
    private int totalLength;

    public MyStack() {
        size = 100;
        theStack = new String[size];
        top = -1;
    }

    public MyStack(int stringSize) {
        size = 100;
        theStack = new String[stringSize];
        top = 0;
    }


    public void push(String symbol) {
        theStack[top] = symbol;
        top++;
    }

    public String pop() {
        return theStack[--top];

    }

    public boolean isEmpty() {
        if (top == -1) {
            return true;
        }else {
            return false;
        }
    }

    public int length() {
        return totalLength = top;
    }

    public void print() {
        for(int i=0;i<=top;i++){
            System.out.print(theStack[i]+ " ");
        }
        System.out.println();
    }
}

PostfixError Class

public class PostfixError extends Exception{
    public PostfixError(String message) {
        super(message);
    }    
}

You call System.exit ; this immediately exits; your 'finally' block is NOT run. As a consequence, your BufferedWriter is just abandoned; neither flush() nor close() is invoked on it which means it won't flush its buffer into the file.

SOLUTION: Don't call System.exit there.

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