java - 使用 Java 写入文本文件被缩短了

标签 java file text save

我有一个程序从一个文本文件(目前长 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);
}

最佳答案

写入文件是缓冲的。您必须在写入结束时 close()flush() 您的编写器 (saveFile)。

更好的是:您应该在 finally block 中对编写器执行 close()

关于java - 使用 Java 写入文本文件被缩短了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804595/

相关文章:

java - 写入文件但仅保存最后一行

python - 这是从哪里来的 : -*- coding: utf-8 -*-

java - Lucene 4.0 索引编写器 - 创建单个索引

java - 是否有 -noverify jvm 标志的替代方案?

java - spring mvc中对象的绑定(bind)列表

java - 轴2问题: WSHandler: Check Signature confirmation: stored SV vector not empty

java - 将Akka与现有Java项目集成的示例

java - Spring 邮件添加附件

javascript - 如何让用户将 html 输入文本区域?如果他们尝试将 &lt;textarea&gt; 包含在文本区域内,我会遇到问题

Peter Norvig 的拼写校正器的 Haskell 版本慢得令人难以置信