RapsFan1981
RapsFan1981

Reputation: 1197

How can I alter ths code to correctly save a file if it exists?

Currently I can load a text file into an EditText. If the text is changed and the user attempts to open a new file a "save first?" dialog is displayed. The problem I'm having is instead of saving the working file (currentFile) it saves over the file to be opened.

Where am I going wrong?

   File currentFile;
public boolean changed;
public boolean exists;
    ...

                private void openFile(final File aFile){
                    String nullChk = et.getText().toString();
                    exists = true;

                if(!changed || nullChk.matches("")){
                     try {
                            et.setText(new Scanner(aFile).useDelimiter("\\Z").next());
                            changed=false;
                            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                            e.printStackTrace();
                            }   
                     }else{
                        AlertDialog.Builder alert = new AlertDialog.Builder(this);

                        alert.setTitle("Save first?");
                        alert.setMessage("(Will be saved in the current working directory)");

                        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                         String temptxt = et.getText().toString();

                        if(exists){
                            saveFile(aFile.getPath(), temptxt);
                        }else{
                            saveAs();
                        }
                          }
                        });
                        final File tempFile = aFile;
                        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int whichButton) {
                              try {
                                    et.setText(new Scanner(tempFile).useDelimiter("\\Z").next());
                                } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            changed=false;
                          }
                        });

                        alert.show();
                        //currentFile = aFile;
                     }
                }

                private void saveFile(String sFileName, String sBody){
                    //Toast.makeText(this, exists +"", Toast.LENGTH_SHORT).show();
                    if (exists) {
                        try {
                            File tempfile = new File(sFileName);
                            FileWriter writer = new FileWriter(tempfile);
                            writer.write(sBody);
                            writer.flush();
                            writer.close();
                            changed=false;
                            Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                            return;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{
                        Toast.makeText(this, "Save as", Toast.LENGTH_SHORT).show();
                        saveAs();
                    }

                }

                private void saveAs(){
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);

                    alert.setTitle("Save as");
                    alert.setMessage("(Will be saved in the current working directory)");

                    // Set an EditText view to get user input 
                    final EditText input = new EditText(this);
                    alert.setView(input);

                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                      String value = input.getText().toString();
                      String tmpText = et.getText().toString();

                      try {
                        File tempfile = new File(currentDirectory, value);
                        FileWriter writer = new FileWriter(tempfile);
                          writer.write(tmpText);
                          writer.flush();
                          writer.close();
                          changed=false; 
                          fill(currentDirectory.listFiles());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                    e.printStackTrace();
                    }
                      }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                          startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
                          dialog.dismiss();
                  }
                    });
                    alert.show();
                }

Upvotes: 0

Views: 76

Answers (1)

AwadKab
AwadKab

Reputation: 3064

I think you use same file to open and save files.

When you open file you use aFile as parameter openFile(final File aFile) and use aFile in

et.setText(new Scanner(aFile).useDelimiter("\\Z").next());

and

saveFile(aFile.getPath(), temptxt);.

Upvotes: 1

Related Questions