-2
public void saveList(Vector<Vector> table_data){
ArrayList<String> output_list = new ArrayList<String>();
for(int i=0;i<table_data.capacity();i++){
String temp="";
Vector tempVector = (Vector) table_data.elementAt(i);
tempVector.trimToSize();
for(int v=0;v<tempVector.capacity();v++){
temp+=((String)tempVector.elementAt(v))+" ";
}
temp = temp.trim();
System.out.println(temp);
output_list.add(temp);
}
BufferedWriter bw = null;
FileWriter fw = null;
try{
fw = new FileWriter(output_filename,false);
bw= new BufferedWriter(fw);
for(String i : output_list){
bw.write(i);
bw.newLine();
}
}
catch(FileNotFoundException e){}
finally{
if (bw != null) {
try {
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
這是我的代碼重寫文件。每次點擊它時,都會有一個JButton調用這個函數。它傳遞來自JTable的向量。該文件應該總是被覆蓋。但實際上只有在我第一次點擊按鈕時纔會覆蓋。有什麼問題,我該如何解決?Java覆蓋文件
歡迎,你可以創建一個更多[mcve]比這個。 – AxelH
拋出了什麼異常? – EJP
由於我沒有看到問題(除了拋出IOException異常),我用一個簡單的String數組運行了你的代碼。每次運行該文件時都會使用新值重新創建該文件。所以你應該更新你的問題,如果有什麼不工作 – AxelH