0

目前我可以将文本文件加载到 EditText 中。如果文本被更改并且用户尝试打开一个新文件“先保存?” 显示对话框。我遇到的问题不是保存工作文件(currentFile)而是保存在要打开的文件上。

我哪里错了?

   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();
                }
4

1 回答 1

1

我认为您使用相同的文件来打开和保存文件。

当您打开文件时,您使用 aFile 作为参数openFile(final File aFile)并在

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

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

于 2013-03-09T01:10:49.373 回答