Java.io.FileNotFoundException when using RandomAccessFile to create a file

I encounter a FileNotFoundException when I try to create a file using RandomAccessFile:

RandomAccessFile file = new RandomAccessFile("/test.jpg", "rw"); 

I do not know how to get around this. It drives me crazy.

thanks

+6
java android filenotfoundexception random-access
source share
3 answers

Try

 RandomAccessFile file = new RandomAccessFile(new File(getFilesDir(), "test.jpg"), "rw"); 
+7
source share

From the documentation:

FileNotFoundException - if the mode is "r", but the given file object does not denote an existing regular file, or if the mode starts with "rw", but the given file object does not denote an existing, writable regular file and a new regular file of this name cannot be created or if there is some other error when opening or creating a file

Can you create such a file in other ways? Do you work in an environment where "/" stands for the root directory?

+1
source share

In fact, this error occurs when we specify only the file name

 String fileName="Shiva.txt" String Directory = Environment.getExternalStorageDirectory() + File.separator + "OneSecondMoments" + File.seseparator + fileName 

eg:

 RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw"); 

Give the path of the whole directory, for example

 RandomAccessFile randomAccessFile = new RandomAccessFile(Directory, "rw"); 
+1
source share

All Articles