开发者

Program throws an exception when I try to create a file in Android

I'm trying to open a file and creat it if it does not exist. My code throws an exception that says file does not exist, but I thought with the rw option, it will creat it

// save dATA   
try {
    RandomAccessFile f=new RandomAccessFile("ted.dat", "rw");
    f.write(cGlobals.mBoard[0], 0, cGlobals.fBoardSize开发者_如何学JAVA* cGlobals.fBoardAmount);
    f.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();


I am sorry, but I think it should not work. Although Android's javadoc does not mention this here is what I saw in J2SE's javadoc:

 * @exception FileNotFoundException
 *            if the mode is <tt>"r"</tt> but the given string does not
 *            denote an existing regular file, or if the mode begins with
 *            <tt>"rw"</tt> but the given string 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

It means that if your mode includes "r" (i.e. it is "r" or "wr") the file should exist.


Yes, what you are trying to do should work. But are you writing where, exactly? Try to write in the SD Card, changing your path to "/sdcard/<yourCustomFolder>/ted.dat".

Also, I think you AndroidManifest.xml should have the following:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜