I want to overwriting file that I have saved before, my saveAs
code:
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?");
}
}
And my save code (that must be overwriting exist file)
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());
}
}
But when I save, the file will saved to "null.txt"
This is not what I want. I would like to overwrite the file that i have saved before.
Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.
Overwriting is the rewriting or replacing of files and other data in a computer system or database with new data. One common example of this is receiving an alert in Microsoft Word that a file with the same name already exists and being prompted to choose whether you want to restore the old file or save the new one.
NET core 3.0 and later versions, you can call Move String, String, Boolean setting the parameter to overwrite to true, which will replace the file if it exists. In all . NET versions, you can call delete(string) before calling Move, which will only delete the file if it exists.
Pass false as 2nd argument, to set append to false, so that you will overwrite the existing file:
FileOutputStream output = new FileOutputStream("output", false);
Check out the constructor documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With