2

I made a file using :

FileOutputStream fos = app.openFileOutput("FileOne.txt", Context.MODE_APPEND); 

Later in the same program, I try to open this file using:

PrintWriter writer = new PrintWriter(context.getFileStreamPath("FileOne.txt")); 

but doing it give this exception in the LogCat:

06-05 09:49:32.230: D/error155 java.lang.RuntimeException: File not found(7707): [ 06-05 09:49:32.240 7707: 7736 D/ java.io.FileNotFoundException: /FileOne.txt: open failed: EROFS (Read-only file) 

I have checked the internal storage, and the file does exist. How to open the file correctly?

10
  • did you read the error message to the end? the file is ro Commented Jun 5, 2013 at 10:07
  • 2
    Or why the File API sucks. If you use Java 7, make yourself a favor and use the new Files API. FileNotFoundException is also thrown if the file exists but you don't have the necessary credentials for what you are trying to do! Commented Jun 5, 2013 at 10:08
  • 1
    First check the file by simply File file = new File(path); file.exists(); may be the path that your providing is different.Also do check file.read() and file.write() permissions Commented Jun 5, 2013 at 10:08
  • 1
    Why dont you define a path for the file and use the path for opening it? Commented Jun 5, 2013 at 10:09
  • 1
    @fge - the OS is specified in the tags, and is ultimately more important than the fact that the language is Java. java.nio.files is not available. Commented Jun 5, 2013 at 14:55

5 Answers 5

3

File file = new File(your path);

//Firstly make sure file exists on the path provided by using the below statement :

file.exists();

Check if the file permission allow :

file.canExecute(); – return true, file is executable; false is not. file.canWrite(); – return true, file is writable; false is not. file.canRead(); – return true, file is readable; false is not. 

Set the file permission :

file.setExecutable(boolean); – true, allow execute operations; false to disallow it. file.setReadable(boolean); – true, allow read operations; false to disallow it. file.setWritable(boolean); – true, allow write operations; false to disallow it. 
Sign up to request clarification or add additional context in comments.

8 Comments

This only solved it partially. Now the same code sometimes works and sometimes throws the exception!
@vergilcorleone - that's a meaningless reply. What changes did you make, what status did these queries report in working vs. non-working cases?
@ChrisStratton Using file.setReadable(true); causes the exception to be thrown erratically. That means sometimes the code would work without any error( ie. the file is being read) and sometimes it throws the exception.
And what does file.exists() report?
@ChrisStratton file.exists() reports true
|
1

the file could be found, but it is read only

open failed: EROFS (Read-only file)

The name FileNotFoundException is missleading

1 Comment

Good point. However, I wonder if it is actually read-only (something one would not expect to happen by omission), or if perhaps it is locked as a result of already being open in write mode by the previous call?
0

I have the same issue when using android 11.

FileOutputStream can create file at the first time, and when I delete that image file using file manager, it's actually delete the file but not delete it from the MediaStore entry, so I need to call this

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(YOUR_FILE_PATH))); 

Comments

-1

this file is created in : your root , which is not readable i think .

so provide a path , to create any file in a specified directory :

Edited :

FileOutputStream fos = app.openFileOutput(Environment.getExternalStorageDirectory() + File.separator +"FileOne.txt", Context.MODE_APPEND); 

4 Comments

How is this relevant to the question asked?
Can you please elaborate as to what using your syntax will do differently?
@TusharPandey - untrue, the file is not created in the root directory. The Android docs say instead that it is created in an appropriate place in the app's private directory.
-1

This exception is also thrown when:

  • The File is a actually a Folder.
  • If you don't have the necesary permissions to access the file

Change the permissions of this file to open it (chmod 777 FileOne.txt)

EDIT:

If is an android app, add the following permission to the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

8 Comments

Thanks for the answer. How to get the write permission?
What OS are you using? Windows, Linux, MAC?
This is actually for an android app, but I am coding in Windows 7.
Ok, if its for android, add this permission to the Manifest: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-1 for suggesting file mode 777. Do not make things both executable and globally writable!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.