0

我的程序使用包裹在 FileWriter 周围的 BufferedWriter ,每次运行时都会在某个位置结束。我终其一生都无法弄清楚为什么。

下面是我的代码。我已经考虑过内存泄漏,但我确信这会引发异常(当然,除非我已经阻止它出现)。如果它应该引发错误,我该如何解决?谢谢

import java.io.*;

public class BuffProcess {

static BufferedReader brOne, brTwo;
static FileReader inOne, inTwo;
//static FileInputStream fOne, fTwo;
static FileWriter out;
static BufferedWriter bw;
static StringBuffer sbOne, sbTwo;
static String stOne, stTwo, tech;
static char cOne, cTwo;
static boolean bOne, bTwo;
static int count, intOne, intTwo, intThree, intFour, intFive, intSix, maxSet, lineCount;
static int pCount, check;

public static void p(String s) {

    try {
        inOne = new FileReader(s + ".ck2");
        inTwo = new FileReader(s + ".ck2");
        brOne = new BufferedReader(inOne);
        brTwo = new BufferedReader(inTwo);
        out = new FileWriter(s + "_edit.ck2");
        bw = new BufferedWriter(out);
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException");
    } catch (IOException e) {
        System.out.println("IOException");
    }

    stOne = "Start";
    stTwo = "Start";
    tech = "Pants";
    bOne = true;
    bTwo = false;
    count = 0;
    pCount = 929;

    try {

        while (stOne != null) { //For entire file
            lineCount++;
            //print("Lines processed=" + lineCount);
            stOne = brOne.readLine();
            brTwo.readLine();
            bw.write(stOne); bw.newLine();

            // Find settlements area of file
            if (stOne.contains("name=\"Vestisland\"")) { //Vestisland action (settlements section)

                for (int i = 2; i > 0; i--) {
                    stOne = brOne.readLine();
                    brTwo.readLine();
                    bw.write(stOne); bw.newLine();

                }

                stOne = brOne.readLine();
                brTwo.readLine();

                if (stOne.contains("max_settlements=")) {//For Vestisland only

                    while (bOne) { //Find tech levels
                        stTwo = brTwo.readLine();
                        count++;

                        if (stTwo.contains("technology=")) {

                            for (int i = 4; i > 0; i--) {
                                stTwo = brTwo.readLine();
                                count++;
                            }
                            tech = stTwo;
                            bOne = false;
                        }
                    }
                    bOne = true;
                    maxSet();
                    bw.write(maxSetBlank + maxSet + "\n"); bw.newLine();
                    pCount--;
                    //print("Number of provinces remaining=" + pCount);
                    //print("Number of provinces done=" + (929 - pCount));
                    print("Vestisland" + ++check);

                    while (count !=0) {
                        brOne.readLine();

                        count--;
                    }
                }
                bw.flush();
                out.flush();

                while (pCount > 0) {// For provinces section
                    stOne = brOne.readLine();
                    //print(stOne);
                    brTwo.readLine();
                    bw.write(stOne); bw.newLine();

                    if (stOne.contains("name=")) {
                        names:
                        switch (stOne) {

                        case nullOne: case nullTwo: case nullThree: case nullFour: case nullFive:
                            print(test + "null");
                            pCount--;
                            //print("Number of provinces remaining=" + pCount);
                            //print("Number of provinces done=" + (929 - pCount));
                            print("Desert" + ++check);
                            break names;

                        default:

                            for (int i = 2; i > 0; i--) {
                                stOne = brOne.readLine();
                                brTwo.readLine();
                                bw.write(stOne); bw.newLine();
                            }

                            stOne = brOne.readLine(); print(stOne);
                            brTwo.readLine();

                            if (!stOne.contains("max_settlements")) {

                                while (bOne) { //Find tech levels
                                    stTwo = brTwo.readLine();
                                    count++;

                                    if (stTwo.contains("technology=")) {

                                        for (int i = 4; i > 0; i--) {
                                            stTwo = brTwo.readLine();
                                            count++;
                                        }
                                        tech = stTwo;
                                        bOne = false;
                                    }
                                } //Find tech levels
                                bOne = true;
                                maxSet();
                                bw.write(maxSetBlank + maxSet + "\n"); bw.newLine();
                                bw.write(stOne); bw.newLine();
                                pCount--;
                                //print("Number of provinces remaining=" + pCount);
                                //print("Number of provinces done=" + (929 - pCount));
                                print("Without max_settlements" + ++check);
                            } else {

                                while (bOne) { //Find tech levels
                                    stTwo = brTwo.readLine();
                                    //print(stTwo);
                                    count++;

                                    if (stTwo.contains("technology=")) {

                                        for (int i = 4; i > 0; i--) {
                                            stTwo = brTwo.readLine();
                                            //print(stTwo);
                                            count++;
                                        }
                                        tech = stTwo;
                                        bOne = false;
                                    }
                                }
                                bOne = true;
                                maxSet();
                                bw.write(maxSetBlank + maxSet + "\n"); bw.newLine();
                                pCount--;
                                //print("Number of provinces remaining=" + pCount);
                                //print("Number of provinces done=" + (929 - pCount));
                                print("With max_settlements" + ++check);
                            }
                            break names;
                        }
                }

            } //While provinces remain

                try {
                    while (stOne != null) {
                        bw.write(brOne.readLine()); bw.newLine();
                    }
                } catch (NullPointerException e) {
                    stOne = null;
                }

        }
        }
    } catch (IOException e) {
        System.out.println("IOException");
    } catch (NullPointerException e) {

    } finally {
        try { bw.close(); } catch (IOException e) {
            System.out.println("IOException");
        } finally { }
    }
    System.out.println("Provinces=" + pCount);
}

private static void maxSet() {
    int avg;
    intOne = Integer.parseInt(tech.substring(16, 17));
    intTwo = Integer.parseInt(tech.substring(18, 19));
    intThree = Integer.parseInt(tech.substring(22, 23));
    intFour = Integer.parseInt(tech.substring(24, 25));
    intFive = Integer.parseInt(tech.substring(30, 31));
    intSix = Integer.parseInt(tech.substring(46, 47));
    avg = Math.round((intOne+intTwo+intThree+intFour+intFive+intSix)/6);
    maxSet = avg + 2;
    //print(maxSet);
}

private static void print(Object o) {
    System.out.println(o);
}

static int pCheck = 0;
static String test = "This line is reached";
static final String maxSetBlank = "\t\tmax_settlements=";
static final String nullOne = "\t\tname=\"Western Sahara\"\n";
static final String nullTwo = "\t\tname=\"Adrar Plateau\"\n";
static final String nullThree = "\t\tname=\"Siberia\"\n";
static final String nullFour = "\t\tname=\"Sahara Desert\"\n";
static final String nullFive = "\t\tname=\"Sharawrah\"\n";

}

4

3 回答 3

3

我怀疑它抛出了你忽略的 NullPointerException 。

我建议删除所有异常捕获并让方法处理throws这些异常。只有当你打算对它们做一些有用的事情时,才值得捕获一个异常。

顺便说一句:我怀疑您的任何字段都不需要是静态的,如果可以的话,我会制作局部变量。

于 2013-01-31T18:00:43.157 回答
0

您在底部附近的捕获 NullPointerException 没有打印任何内容。

忠告...使用自动记录错误的 IDE,例如 Eclipse。它将使您的生活轻松十亿倍。

于 2013-01-31T18:08:14.497 回答
0

首先,在您的 catch 块中打印堆栈跟踪,例如,替换

} catch (IOException e) {
    System.out.println("IOException");
}

经过

} catch (IOException e) {
    e.printStackTrace();
}

并避免在不重新抛出或记录异常的情况下捕获异常。您当前在调试过程中面临的问题就是后果。

顺便说一句,如果您在某一时刻不知道如何处理异常,请不要捕获它。例如,IOException一般不应该被抓住和忽视。

也不要捕获逻辑错误,如NullpointerException,或. 这些几乎总是编程错误。当你抓住它们并忽略它们时,你隐藏了问题,但你还没有解决它。IllegalStateExceptionIllegalArgumentExceptionAssertionError

于 2013-01-31T18:22:28.743 回答