java.io.FileNotFoundException when using RandomAccessFile to create file

…衆ロ難τιáo~ 提交于 2019-12-22 05:26:12

问题


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


回答1:


Try

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



回答2:


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?




回答3:


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");


来源:https://stackoverflow.com/questions/3597704/java-io-filenotfoundexception-when-using-randomaccessfile-to-create-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!