0

This function creates a file but I can't figure out where is the file created and if someone has a solution to create a file in a particular directory from the external storage is very welcomed :) thanks a lot

private void writeFileToInternalStorage() {
    String eol = System.getProperty("line.separator");
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile", MODE_WORLD_WRITEABLE)));
      writer.write("This is a test1." + eol);
      writer.write("This is a test2." + eol);
    } catch (Exception e) {
            e.printStackTrace();
    } finally {
      if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
      }
    }
}
exilonX
  • 1,712
  • 3
  • 26
  • 50

2 Answers2

0

It will be created on internal folder: /data/data/com.package.name/ You cannot access that folder using file browser.

If you want to easily access the file you can try to create it on SD card:

/*...*/
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = baseDir + "/"+ "myFile.txt";
FileOutputStream writer = null;
try {
    writer = new FileOutputStream(fileName);
    writer.write("This is a test1." + eol);
    /*...*/
Caner
  • 57,267
  • 35
  • 174
  • 180
  • If I do it like this then I get an Illegal argument exception myFile.txt contains a path separator – exilonX Jun 26 '12 at 14:59
  • true, there was a mistake. I changed eol with "/". check out my edited answer – Caner Jun 26 '12 at 15:01
  • almost the same now Illegal argument exception /mnt/sdcard/myFile.txt contains a path separator so... what can I do ? – exilonX Jun 26 '12 at 15:04
  • right, it seems that `openFileOutput` does not allow "\" in file name so you cannot use it to write to sd card. i changed my code once again. check it. – Caner Jun 26 '12 at 15:14
0

for query

  • Where will be a file created

it will create in Internal Storage as function name said and that will be like /data/data/yourApp_package_as_in_manifest/ (can see in DDMS)

for query

  • if someone has a solution to create a file in a particular directory from the external storage is very welcomed

as per link Write a file in external storage in Android .........

** Method to check whether external media available and writable. This is adapted from
       http://developer.android.com/guide/topics/data/data-storage.html#filesExternal */

     private void checkExternalMedia(){
          boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // Can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // Can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Can't read or write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }   
        tv.append("\n\nExternal Media: readable="
                +mExternalStorageAvailable+" writable="+mExternalStorageWriteable);
    }

    /** Method to write ascii text characters to file on SD card. Note that you must add a 
       WRITE_EXTERNAL_STORAGE permission to the manifest file or this method will throw
       a FileNotFound Exception because you won't have write permission. */

    private void writeToSDFile(){

        // Find the root of the external storage.
        // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

        File root = android.os.Environment.getExternalStorageDirectory(); 
        tv.append("\nExternal file system root: "+root);

        // See https://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();
        }   
        tv.append("\n\nFile written to "+file);
    }

and also add a WRITE_EXTERNAL_STORAGE permission to the manifest

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36