FileWriter不写入输出文件

时间:2019-03-03 02:56:31

标签: java stack filewriter

我可以运行此代码,只需将其打印到控制台,然后一切都可以正常打印,然后将所有System.out.println更改为outputStream.write (“某些字符串”)根本不会写入输出文件。它只是空白。第一个文件是主文件。然后我的其他类是MyStack和PostfixError。我将把它们包括在此之下。该代码从在命令行中输入的输入文件中获取输入,然后将其输出到也由命令行指定的文件中。输入文件应采用Postfix格式,例如ABC +-或AB + C。如果不是这种格式,则会产生错误。如果输入采用的是这种格式,那么代码将采用该输入,并编写指令(如果我们使用的是只有一个寄存器的计算机,则需要使用该指令来评估后缀表达式)。

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类

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类

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

1 个答案:

答案 0 :(得分:0)

您致电System.exit;这立即退出;您的“最终”代码块未运行。结果,您的BufferedWriter被遗弃了; flush()close()都不会被调用,这意味着它不会将其缓冲区刷新到文件中。

解决方案:请勿在此处调用System.exit。