java - openFileInput() and/or openFileOutput() i/o streams silently failing -
i've been fooling around android platform, messing around different ways of storing data. right using context
methods openfileinput()
, openfileoutput()
.
i created file called default documentation these 2 methods told me. here's example code (these examples replicas of did, aware filename , variables named differently):
openfileoutput()...
context cont = /* defined somewhere */; string filename = "hello_file"; string string = "hello world!"; fileoutputstream fos = cont.openfileoutput(filename, context.mode_private); fos.write(string.getbytes()); fos.flush(); fos.close();
openfileinput()...
fileinputstream fis = cont.openfileinput("hello_file"); byte[] buffer = new byte[(int) fis.getchannel().size()]; fis.read(buffer); string str= ""; for(byte b:buffer) str+=(char)b; fis.close();
in these code snippets, "hello world" should written file "hello_file", , should what's in str
. problem i'm having code no matter write file, fileinputreader
picks nothing.
i scrolled through permissions listed in android documentation, not find internal storage (and pretty sure don't need permission this).
the bottom line is, don't understand why fileinputwriter
isn't writing or why fileinputreader
isn't reading (i don't know is) when code runs fine , without error.
i write answer, because getting comment. i've tried code - , well, works fine.
the thing can imagine is, device has problem. in case expect exception...
still can do, copy code , check log. see if works, or if might exception. check how internal memory got in device (is real or emulated?)
if emulated might small such small file.
here's code, put onresume()
string filename = "hello_file"; string string = "hello world!"; try { fileoutputstream fos = openfileoutput(filename, context.mode_private); fos.write(string.getbytes()); fos.flush(); fos.close(); } catch (ioexception e) { log.e("stackoverflow", e.getmessage(), e); } try { fileinputstream fis = openfileinput("hello_file"); byte[] buffer = new byte[(int) fis.getchannel().size()]; fis.read(buffer); string str= ""; for(byte b:buffer) str+=(char)b; fis.close(); log.i("stackoverflow", string.format("got: [%s]", str)); } catch (ioexception e) { log.e("stackoverflow", e.getmessage(), e); }
the output:
08-16 08:31:38.748: i/stackoverflow(915): got: [hello world!]
is there category of: "helpful, not sovling problem"?
Comments
Post a Comment