4

I found out that you can use something like this to create a file:

FileOutputStream  fs = openFileOutput("/test.in", MODE_WORLD_WRITEABLE);
String s = "[Head]\r\n";
s += "Type=2";
byte[] buffer = s.getBytes();
fs.write(buffer);
fs.close();

When running the above code I get an IllegalArgumentException stating:

java.lang.IllegalArgumentException: File /test.in contains a path separator

and I'm guessing the "/" is not appreciated. I wanted the "/" since I need to write the file to the root directory of the device, as stated in the API in trying to follow:

A request is a textfile (UNICODE) with the file extension ".in". The application reads and parses the .in file when it's placed in root directory on the mobile device.

Question is: how do I place a file in the root-directory? I have been looking around for an answer, but haven't found one yet.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ted
  • 19,727
  • 35
  • 96
  • 154

3 Answers3

8

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

Elliott Hughes
  • 4,595
  • 2
  • 23
  • 21
  • Thanks for the answer. Its the only API available to "communicate" with this Application. You communicate by creating text-files in the root directory as quoted above. So its not the wrong documentation - its the only one that exists. I will contact them and ask how they expect that to work on Android =) – Ted Jan 17 '10 at 15:57
  • 2
    Are you sure it doesn't need you to write to the root directory of the SD card? – Mark B Jan 18 '10 at 18:00
4

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();
    }
user865197
  • 179
  • 2
  • 2
  • 2
    Where does "write(OutputStream, Byte[])" get declared? I had to use myOutput.write(Byte[]) instead to make it work. – ArtOfWarfare Aug 21 '12 at 14:10
0

I faced the same problem today

java.lang.IllegalArgumentException: File /test.txt contains a path separator

and when i tried the following it worked.

           File inputFile = new File("/storage/new/test.txt");
           FileInputStream isr = new FileInputStream(inputFile);
           DataInputStream in = new DataInputStream(isr);

           if(!inputFile.exists()){

                   Log.v("FILE","InputFile not available");

            }
           else
           {
               while((line = in.readLine()) != null)
               {
                   ........ //parse 
               }
           }              

(Btw, I was getting this problem outside /root dir and while searching i saw this post)

m4n07
  • 2,267
  • 12
  • 50
  • 65
  • Thx for the answer. If I ever get my hands into android development again, I will try it out =) – Ted Apr 02 '11 at 13:38