java - Android : nonwritablechannelException and ClosedChannelException -
i'm using filelock
, don't know why meet nonwritablechannelexception exception
:
public static list<string> readfromfile(context ctx, string filename) { try { fileinputstream fis = ctx.openfileinput(filename); // lock file filelock lock = fis.getchannel().trylock(); // exception here // unlock file lock.release(); return null; } catch (exception e) { log.i(tag, "cannot read file"); e.printstacktrace(); } return null; }
and meet exception when writting file: exception closedchannelexception
public static boolean savetofile(context ctx, list<string> lst, string filename) { try { fileoutputstream fos = ctx.openfileoutput(filename, context.mode_private); // lock file filelock lock = fos.getchannel().lock(); packageobject obj = new packageobject(lst); objectoutputstream writer = new objectoutputstream(fos); writer.writeobject(obj); writer.close(); // unlock file lock.release(); // exception @ line fos.close(); return true; } catch (exception e) { log.i(tag, "cannot write file"); e.printstacktrace(); } return false; }
on android developer page, explain exception is:
a nonwritablechannelexception thrown when attempting write channel not open writing.
but still cannot explain why. please me figure out why meet exception please.
thanks :)
"a nonwritablechannelexception thrown when attempting write channel not open writing."
you opened file/channel using openfileinput
... opening reading not writing. if want take lock on file, have open write using openfileoutput
instead ... or maybe well.
Comments
Post a Comment