Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite existing file? [duplicate]

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.

like image 217
Andhika Kurnia Aufa Azham Avatar asked Nov 22 '14 04:11

Andhika Kurnia Aufa Azham


People also ask

How do I overwrite a copy of a file?

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.

What does it mean to overwrite existing files?

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.

How do I overwrite a file in C#?

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.


1 Answers

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:

like image 92
Ehsan Avatar answered Oct 20 '22 10:10

Ehsan