3

我有一个程序从一个文本文件(目前有 653 行长)中读取,所有文件都用逗号分隔。但是当我将文件保存到新位置时,它只保存了 490 行。新创建的文本文件中的最后一行似乎也被切成了两半。关于可能是什么问题的任何想法?

这是我用来打开和排序列表中的数据的代码:

try {
    scanner = new Scanner(file);

    // Put the database into an array and 
    // Make sure each String array is 13 in length
    while (scanner.hasNext()) {
        line = scanner.nextLine();
        word = line.split(",");

        if (word.length < 13) {
            String[] word2 = {"","","","","","","","","","","","",""};
            for (int i = 0; i < word.length; i++) {
                word2[i] = word[i];
            }
            dataBaseArray.add(word2);
        }
        else {
            dataBaseArray.add(word);
        }
    }
}
catch (FileNotFoundException exc) {
    JOptionPane.showMessageDialog(this, "File cannot be found.", "error finding file", JOptionPane.ERROR_MESSAGE);
}

// Splitting the database into vacant numbers/dead lines/vacant cubicles
for (int i = 0; i < dataBaseArray.size(); i++) {
    if (dataBaseArray.get(i)[8].equals("VACANT")) {
        vacantNums.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[4].equals("DEAD")) {
        deadLines.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[6].equals("") && dataBaseArray.get(i)[7].equals("")) {
        vacantCubs.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[7].equals("")) {
        people.add(dataBaseArray.get(i));
    }
    else {
        people.add(dataBaseArray.get(i));
    }
}

// Resetting the DB Array to put the values back in it
dataBaseArray = new ArrayList<>();

// Ordering the arrays I want them to appear in the list
// Orering the people to appear in alphabetical order
Collections.sort(people, new Comparator<String[]>() {

    @Override
    public int compare(String[] strings, String[] otherStrings) {
        return strings[7].compareTo(otherStrings[7]);
    }
});

// Put the people in the DB Array
for (int i = 0; i < people.size(); i++) {
    dataBaseArray.add(people.get(i));
}

// Put the vacant numbers in the AB Array
for (int i = 0; i < vacantNums.size(); i++) {
    dataBaseArray.add(vacantNums.get(i));
}

// Put the vacant cubicles in the AB Array
for (int i = 0; i < vacantCubs.size(); i++) {
    dataBaseArray.add(vacantCubs.get(i));
}

// Put the dead lines in the AB Array
for (int i = 0; i < deadLines.size(); i++) {
    dataBaseArray.add(deadLines.get(i));
}

list = new String[dataBaseArray.size()];

// Add the DB Array to the list
for (int i = 0; i < list.length; i++) {
    if (dataBaseArray.get(i)[8].equals("VACANT")) {
        list[i] = "VACANT";
    }
    else if (dataBaseArray.get(i)[4].equals("DEAD")) {
        list[i] = "DEAD";
    }
    else if (dataBaseArray.get(i)[6].equals("") && dataBaseArray.get(i)[7].equals("")) {
        list[i] = "Vacant Cubicle";
    }
    else if (dataBaseArray.get(i)[7].equals("")) {
        list[i] = dataBaseArray.get(i)[6];
    }
    else {
        list[i] = dataBaseArray.get(i)[7] + ", " + dataBaseArray.get(i)[6];
    }
}

// Populate the list
lstAdvance.setListData(list);

这是我用来保存文件的内容:

try {
    saveFile = new FileWriter("Save Location");
    String newLine = System.getProperty("line.separator");

    for (int i = 0; i < dataBaseArray.size(); i++) {
        for (int j = 0; j < dataBaseArray.get(i).length; j++) {
            saveFile.append(dataBaseArray.get(i)[j] + ",");
        }
        saveFile.append(newLine);
    }
}
catch (IOException exc) {
    JOptionPane.showMessageDialog(this,"error", "error", JOptionPane.ERROR_MESSAGE);
}
4

3 回答 3

5

写入文件被缓冲。您必须在写作结束时close()或您的作家(saveFile)。flush()

更好的是:你应该在街区close()里对你的作家做。finally

于 2012-10-09T16:44:19.327 回答
0

尝试使用FileWriterand BufferedWriter....

File f = new File("Your_Path");

FileWriter fw = new FileWriter(f);

BufferedWriter bw = new BufferedWriter(fw);

是的..它非常重要bw.close() (关闭缓冲区)

于 2012-10-09T16:48:24.450 回答
0

看到这个问题: Java FileWriter with append mode

问题是您的 FileWriter 对象需要是 "append mode" 。然后,您使用“write”方法而不是“append”方法追加到文件。使用 finally catch 子句调用 "close" 。你不需要冲洗(我不认为)。

于 2012-10-09T16:49:56.557 回答