3

I have a program reading from a text file (currently 653 lines long) all separated by a comma. But when I go to save the file to a new location, it only saves 490 lines. It also seems that the last line in the newly created text file is cut in half. Any ideas on what might be the problem?

Here is the code that I used to open and sort the data in the list:

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);

Here is what I used to save the file:

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);
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Caveman42
  • 679
  • 2
  • 13
  • 35

3 Answers3

5

Writing to a file is buffered. You have to close() or flush() your writer (saveFile) at the end of writing.

Even better: you should do close() on your writer in the finally block.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Kylo
  • 2,300
  • 1
  • 19
  • 24
0

Try it using the FileWriter and BufferedWriter....

File f = new File("Your_Path");

FileWriter fw = new FileWriter(f);

BufferedWriter bw = new BufferedWriter(fw);

And yes..its very important to do bw.close() (Closing the Buffer)

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

See this question : Java FileWriter with append mode

The problem is that your FileWriter object needs to be "append mode" . Then, you append to the file with the "write" method rather than the "append" method. Use a finally catch clause to call "close" . You don't need to flush ( I dont think).

Community
  • 1
  • 1
djangofan
  • 28,471
  • 61
  • 196
  • 289