我想重寫,我以前保存的文件,我另存爲代碼:如何覆蓋現有文件的Java
public void SaveAs(){
judul = jTextJudul.getText();
s = area.getText();
if(s.length()>0){//jika s terisi
try {
dialog = new FileDialog(this,"Save File As",FileDialog.SAVE);
dialog.setFile(judul+".txt");
dialog.setVisible(true);
path=dialog.getDirectory()+dialog.getFile();
FileOutputStream fos=new FileOutputStream(path);
System.out.println(s);
byte[] b=s.getBytes();
fos.write(b);
fos.close();
setTitle(name);
}
catch(Exception e){
JOptionPane.showMessageDialog(this,e.getMessage());
}
}
else{
JOptionPane.showMessageDialog(this,"Apa anda yakin menyimpan file kosong?");
}
}
而且我節省代碼(必須覆蓋存在的文件)
public void Save(){
dialog = new FileDialog(this,"Save",FileDialog.SAVE);
file = new File(path+".txt");
s = area.getText();
try{
// Create file
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(s);
//Close the output stream
out.close();
}
catch (IOException e){
JOptionPane.showMessageDialog(this,e.getMessage());
}
}
但是當我保存時,該文件將保存爲「null.txt」 這不是我想要的。我想覆蓋之前保存的文件。
我沒有找到以前這個問題。 :)但是,感謝 – 2014-11-22 04:31:26