java.io.FileNotFoundException when using RandomAccessFile to create file
Asked Answered
X

3

6

I'm encountering a FileNotFoundException when I try to make a file using RandomAccessFile:

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

I don't now how to get around this. It's driving me nuts.

Thanks

Xyloid answered 30/8, 2010 at 3:37 Comment(2)
Do you actually have permission to create a file on the filesystem root? Try with just test.jpgBiff
"/" is there by accident... I do not use it in the code. The app should create the file in the app directory not the root directory.Xyloid
D
8

Try

RandomAccessFile file = new RandomAccessFile(new File(getFilesDir(), "test.jpg"),
        "rw");
Demb answered 30/8, 2010 at 4:12 Comment(2)
Thanks mate. One more thing... further down I'm trying to access it again to convert to a Bitmap: bmImg = BitmapFactory.decodeFile("wallpaperSwitch.jpg"); I'm getting error again, how do I do it then?Xyloid
Bitmap bmImg = BitmapFactory.decodeStream(openFileInput("wallpaperSwitch.jpg")); Try that.Demb
M
1

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 begins with "rw" but the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

Are you able to create such a file by other means? Are you working in an environment where "/" denotes the root directory?

Motley answered 30/8, 2010 at 3:52 Comment(1)
I haven't tried creating the file using other means, but I'm assuming I would get the same result. The "/" is there by accident... please ignore it.Xyloid
R
1

Actually this error occurs when we only give the file name

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

for example:

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

Give the Path of whole Directory, for example

RandomAccessFile randomAccessFile = new RandomAccessFile(Directory, "rw");
Rede answered 9/1, 2013 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.