0

I am working on an Android app and I would like to write into a text file. But after clicking the write button, the app shows me an error message and stops. The error is

IllegalArgumentException: File mnt/sdcard/test.txt contains a path separator

I have used the following code:

try {
    final String TESTSTRING = new String("Hello Android");

    FileOutputStream fOut = openFileOutput("mnt/sdcard/test.txt",
                                               MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(TESTSTRING);

    osw.flush();
    osw.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}
adamhala007
  • 703
  • 4
  • 11
  • 22

3 Answers3

2

Make sure you have: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your manifest as well.

 package com.example.fileio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class FilioActivity extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "filename.txt");
    tv.append("\nExternal file system root: "+ dir);
    try {
        FileOutputStream f = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
        PrintStream p = new PrintStream(f);
        p.print(s);
        p.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("\nFile written to \n"+file);


}
}
1
 Try something like this      

       try {
         File myFile = new File("/sdcard/sample.txt");

         // added extra

         myFile.createNewFile(); 

         FileOutputStream fOut = new FileOutputStream(myFile);
         OutputStreamWriter myOutWriter = 
         new OutputStreamWriter(fOut);    
         myOutWriter.close();
         fOut.close();
      }

dont forget add "<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />" in your manifest.

Aravin
  • 4,126
  • 1
  • 23
  • 39
1

WRITE_EXTERNAL_STORAGE is not currently required. This isn't the problem.

The problem is that you're trying to a file with a path to private storage.

openFileOutput() opens a File for writing to your private area. The filename argument can't contain a path. The reference documentation says: "The name of the file to open; can not contain path separators." (see reference doc for openFileOutput). To write to internal storage, simply use openFileOutput("test.txt", Context.MODE_PRIVATE). No reason to set any other mode; your app is the only one that can access the file.

If you want to write to external storage, use getExternalFilesDir(), specify the type of subdirectory (if you want to), and then use normal java.io.* objects (such as File) to write to the file. The reference documentation shows an example. Notice that files in external storage are visible to other apps.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18