LuxuryWaffles
LuxuryWaffles

Reputation: 1708

Android not saving txt file

I am following a tutorial for creating a simple notepad application and I have been stuck for hours trying to figure why isn't my file saving

Everything works fine up to the point when I try to save the txt file, I have also added permissions into the manifest.

The Toast also doesn't appear after I hit the save button

Note Editing Java (Getting file name and text):

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

Backend Java to save note:

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

Upvotes: 1

Views: 133

Answers (2)

1234567
1234567

Reputation: 2485

have you added permission to store file in manifest.xml

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

also create a file first

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

Upvotes: 0

Dhaval Patel
Dhaval Patel

Reputation: 10299

You forgot to create new file before adding content to it.

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

Upvotes: 1

Related Questions