1

When I was tried to create a New File, I got a error message.

Code:

try 
{
    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    Runtime.getRuntime().exec("chmod 777 " + root.getAbsolutePath());
    File myFile = new File(root,"createNew.txt");
    myFile.createNewFile();
}
catch(Exception e)
{
    Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}

Error Message:

java.io.IOException: open failed: EACCES (Permission denied)

/mnt/sdcard Permission as d---------

How do change Permission through programmatically?How do solve. Give me any idea.

Thanks in advance.

Note this point:

When I was used this code in command prompt, file is created successfully, In that code is:

`chmod 777 /mnt/sdcard`
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Sathish Sathish
  • 2,251
  • 3
  • 20
  • 21

3 Answers3

0

Use this to create the file:

FileOutputStream fos= new FileOutputStream(outputPath);
//fos.write(...);
fos.close();

as the Java Doc says:

This method is not generally useful. For creating temporary files, use  
`createTempFile(String, String)` instead. For reading/writing files, use 
FileInputStream, FileOutputStream, or RandomAccessFile, all of which can create 
files. 
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • I need to access only create a new file. Not write anything. Just only create a file. How? – Sathish Sathish Jul 17 '12 at 08:27
  • try to create the output stream and close it without writing, and check http://developer.android.com/reference/java/io/FileOutputStream.html#FileOutputStream(java.io.File) and http://www.anddev.org/working_with_files-t115.html – Nermeen Jul 17 '12 at 08:33
0

1-Use this to get your sdk directory:

Environment.getExternalStorageDirectory().getAbsolutePath();      

2-Or may be your file path has some illegal characters(like blank space),in this case you need to replace them with legal equals characters.for example blank spaces with "\ ".you can see this pages:
Runtime.getRuntime().exec()
http://www.coderanch.com/t/498450/java/java/exec-command-not-able-deal
Runtime.exec on argument containing multiple spaces
Combine paths in Java

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • see my question now. When i was tried to set permission as programmatically, at that time i can't create a new file. When i was tried to set permission through `command prompt` it's working. – Sathish Sathish Jul 17 '12 at 08:11
  • I need to access only create a new file. Not write anything. Just only create a file. – Sathish Sathish Jul 17 '12 at 08:27
0
try {
    File filename = new File(Environment.getExternalStorageDirectory() +
                             "/yourfilename.txt");
    filename.createNewFile();
    FileWriter writer = new FileWriter(filename2); 
    writer.write("Your content");
    writer.flush(); 
    writer.close();
} catch (IOException e) {
    ...
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Shadab
  • 1
  • Please don't promote `e.printStackTrace()`. That's a bad habit. IDEs that put that by default truly hate you. – Kijewski Jul 17 '12 at 21:15