【问题标题】:Writing to a text file using Java gets cut short使用 Java 写入文本文件会被缩短
【发布时间】:2012-10-09 16:40:11
【问题描述】:

我有一个程序从一个用逗号分隔的文本文件(目前为 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);
}

【问题讨论】:

    标签: java file text save


    【解决方案1】:

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

    更好的是:您应该在finally 块中对您的作家执行close()

    【讨论】:

      【解决方案2】:

      尝试使用FileWriterBufferedWriter....

      File f = new File("Your_Path");
      
      FileWriter fw = new FileWriter(f);
      
      BufferedWriter bw = new BufferedWriter(fw);
      

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2015-01-21
          • 2017-04-12
          • 2012-12-11
          • 1970-01-01
          • 2012-07-04
          • 2013-05-18
          • 2021-10-30
          • 1970-01-01
          • 2016-10-06
          相关资源
          最近更新 更多