How to create/write file in the root of the Android device?

Solution 1:

Context.openFileOutput is meant to be used for creating files private to your application. they go in your app's private data directory. you supply a name, not a path: "name The name of the file to open; can not contain path separators".

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)

as for your question, you can't write to / unless you're root:

my-linux-box$ adb shell ls -l -d / drwxr-xr-x root root 2010-01-16 07:42 $

i don't know what your API is that expects you to write to the root directory, but i'm guessing it's not an Android API and you're reading the wrong documentation ;-)

Solution 2:

You can add files with path in private directory like that

    String path = this.getApplicationContext().getFilesDir() + "/testDir/";
    File file = new File(path);
    file.mkdirs();
    path += "testlab.txt";
    OutputStream myOutput;
    try {
    myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
    write(myOutput, new String("TEST").getBytes());
    myOutput.flush();
    myOutput.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }