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

I found 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 I run the above code, I get an IllegalArgumentException:

java.lang.IllegalArgumentException: file /test.in contains the path separator

and I assume that "/" is not appreciated. I wanted "/", since I need to write the file to the root directory of the device, as indicated in the API when trying to follow:

The request is a text file (UNICODE) with the file extension ".in". The application reads and analyzes the .in file when it is in the root directory of the mobile device.

Question: how do I put a file in the root directory? I was looking for an answer, but have not yet found.

+3
3

Context.openFileOutput , . . , : "name , ".

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

, / root:

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

, API , , , API Android, ; -)

+7

,

    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();
    }
+4

.

java.lang.IllegalArgumentException: /test.txt

, .

           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, /root dir )

0

Source: https://habr.com/ru/post/1728785/


All Articles