Android没有保存txt文件

时间:2016-01-19 06:19:13

标签: java android

我正在关注创建一个简单的记事本应用程序的教程,我已经被困了几个小时试图找出为什么我的文件保存

当我尝试保存txt文件时,一切正常,我还在清单中添加了权限。

点击保存按钮后,Toast也不会出现

注意编辑Java(获取文件名和文本):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_save:
            SaveNote();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void SaveNote() {

    if (filename == null) {

        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Note Name");
        alert.setMessage("Please input the note name.");

        // 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();
                filename = value + ".txt";
                new Script_manager(Script_editor.this).SaveNote(filename, et_editor.getText().toString());
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
            }
        });

        alert.show();

    } else {
       new Script_manager(Script_editor.this).SaveNote(filename, et_editor.getText().toString());
    }
}

后端Java保存注释:

private String path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Scripts/";

public void SaveNote(String sFileName, String sBody){
    try
    {
        File root = new File(this.path);
        if (!root.exists()) {
            root.mkdirs();
        }
        File file = new File(root, sFileName);
        FileWriter writer = new FileWriter(file);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this.context, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
        e.printStackTrace();

    }
}

Log Cat:

>01-19 14:06:48.113 21958-21958/com.test.android.notetutorial W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Scripts/dfeff.txt: open failed: ENOENT (No such file or directory)
>01-19 14:06:48.113 21958-21958/com.test.android.notetutorial W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
>01-19 14:06:48.113 21958-21958/com.test.android.notetutorial W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
>01-19 14:06:48.113 21958-21958/com.test.android.notetutorial W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:42)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at com.test.android.notetutorial.Script_manager.SaveNote(Script_manager.java:50)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at com.test.android.notetutorial.Script_editor$1.onClick(Script_editor.java:76)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at android.os.Looper.loop(Looper.java:148)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5525)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at libcore.io.Posix.open(Native Method)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
>01-19 14:06:48.114 21958-21958/com.test.android.notetutorial W/System.err:     ... 12 more

2 个答案:

答案 0 :(得分:1)

您在向其添加内容之前忘记创建新文件。

public void SaveNote(String sFileName, String sBody){
    try {
        File root = new File(this.path);
        if (!root.exists()) {
            root.mkdirs();
        }
        File file = new File(root, sFileName);

        if(!file.exists()){ //Check if file exist 
            // Create new File
            file.createNewFile();
        }

        FileWriter writer = new FileWriter(file);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this.context, "Saved", Toast.LENGTH_SHORT).show();
    } catch(IOException e){
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

您是否已添加存储文件的权限 在manifest.xml中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

还首先创建一个文件

public void generateNoteOnSD(String sFileName, String sBody){
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
         importError = e.getMessage();
         iError();
    }
   }