使用Java写入文本文件会被缩短
我有一个程序从一个文本文件中读取653行,所有行都用逗号分隔。但当我将文件保存到新位置时,它只保存490行。新创建的文本文件中的最后一行似乎也被一分为二。有什么问题吗 下面是我用来打开列表中的数据并对其排序的代码:使用Java写入文本文件会被缩短,java,file,text,save,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 whil
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);
}
写入文件是缓冲的。您必须在写入结束时关闭或刷新writer保存文件
更好的是:您应该在finally块中关闭writer。写入文件是缓冲的。您必须在写入结束时关闭或刷新writer保存文件 更好的是:您应该在finally块中关闭writer。使用FileWriter和BufferedWriter进行尝试 是的..执行bw非常重要。请关闭缓冲区尝试使用FileWriter和BufferedWriter 是的..执行bw非常重要。关闭缓冲区查看以下问题: 问题是您的FileWriter对象需要是追加模式。然后,使用write方法而不是append方法附加到文件中。使用finally catch子句调用close。你不需要冲洗,我不认为。看这个问题: 问题是您的FileWriter对象需要是追加模式。然后,使用write方法而不是append方法附加到文件中。使用finally catch子句调用close。我想你不需要冲洗
File f = new File("Your_Path");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);