使用Java写入文本文件会缩短

时间:2012-10-09 16:40:12

标签: 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);
}

3 个答案:

答案 0 :(得分:3)

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

更好:您应该在close()区块中对作家finally进行操作。

答案 1 :(得分:0)

使用FileWriterBufferedWriter ....

尝试
File f = new File("Your_Path");

FileWriter fw = new FileWriter(f);

BufferedWriter bw = new BufferedWriter(fw);

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

答案 2 :(得分:0)

请参阅此问题:Java FileWriter with append mode

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