0

Here's what I'm doing:

    static void writeToFile(String text) {
    try {
        synchronized (fileLock) {
            File external = Environment.getExternalStorageDirectory();
            String sdcardPath = external.getPath();
            FileWriter filewriter = new FileWriter(sdcardPath
                    + "/Documents/myfile.txt", true);
            BufferedWriter out = new BufferedWriter(filewriter);

            out.write(text);

            out.close();
            filewriter.close();
        }
    } catch (Exception e) {
        android.util.Log.d("failed to save file", e.toString());
    }
}

On open file I get following exception:

java.io.FileNotFoundException: /mnt/sdcard/Documents/myfile.txt: 
    open failed: ENOENT (No such file or directory)
  1. The directory /mnt/sdcard/Documents/ exists according to Root Browres
  2. The application manifest does contain <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  3. myfile.txt does not exist, I want it created or appended

How do I create or append to a file on android?

UPDATE Here is my AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mytest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
tshepang
  • 12,111
  • 21
  • 91
  • 136
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224

4 Answers4

3

first you have to create the file then append to it this is the code to create the file

    sFileName = "Android.txt";
    File root=Environment.getExternalStorageDirectory();

    File textfile = new File(root, sFileName);
    textfile.createNewFile();

Permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Prakhar
  • 2,270
  • 19
  • 26
  • Ok, this works, but than I can't find the file later on in my file system. I should had created `/mnt/sdcard/myfile.txt`, but it's not there – Arsen Zahray Aug 07 '13 at 10:38
1

You can try something like that:

static void writeToFile(String text) {
    try {
        synchronized (fileLock) {
            File external = Environment.getExternalStorageDirectory();
            String sdcardPath = external.getPath();
            File file = new File(sdcardPath + "/Documents/myfile.txt");
            file.createNewFile();
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);

            out.write(text);

            out.close();
            filewriter.close();
        }
    } catch (Exception e) {
        android.util.Log.d("failed to save file", e.toString());
    }
}
Brtle
  • 2,297
  • 1
  • 19
  • 26
1

Try this:

        File storagePath = new File(getExternalCacheDir() + "/dirname/");
        storagePath.mkdirs();
        File myFile = new File(storagePath, fileName);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(
                myFile));
        try {

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

        } finally {
            try {
                out.close();

            } catch (IOException e) {

            }
            try {
                inputStream.close();
            } catch (IOException e) {

            }
        }
M.Salomaa
  • 63
  • 7
1

Use like this:

   File root = android.os.Environment.getExternalStorageDirectory(); 


   // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

   File dir = new File (root.getAbsolutePath() + "/download");
   dir.mkdirs();
   File file = new File(dir, "myData.txt");

   try {
       FileOutputStream f = new FileOutputStream(file);
       PrintWriter pw = new PrintWriter(f);
       pw.println("Hi , How are you");
       pw.println("Hello");
       pw.flush();
       pw.close();
       f.close();
   } catch (FileNotFoundException e) {
       e.printStackTrace();
       Log.i(TAG, "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
   } catch (IOException e) {
       e.printStackTrace();
   }   

OR:

     File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/");

     File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/myFile.txt");        
     if(!direct.exists())
     {
         direct.mkdir();
     }        

     if (!file.exists()) {
             try {
                 file.createNewFile();
                 FileOutputStream f = new FileOutputStream(file);
                 PrintWriter pw = new PrintWriter(f);
                 pw.println("Hi , How are you");
                 pw.println("Hello");
                 pw.flush();
                 pw.close();
                 f.close();

             } catch (IOException e) {
                 e.printStackTrace();
             }}

Hope this will help you.

Nirmal
  • 2,340
  • 21
  • 43
  • the code executes without exceptions, but the files don't appear in the filesystem – Arsen Zahray Aug 07 '13 at 11:08
  • @ArsenZahray its working for me.are you checking in emulator or device?to see the file in emulator go to File Explore-->mnt-->sdcard-->download---->myData.txt. – Nirmal Aug 07 '13 at 11:11
  • The app says that the file was created, and that the directory exists, but Root Browser or file explorer or console emulator can't find those. very strange (( – Arsen Zahray Aug 07 '13 at 11:55