android - Nullpointer returns when trying to create a file -
i have code save float array file future use. when call order savearray("the x",x);
x float array want save, returns null file , not created.
public void savearray(string filename, float[] array) { try { fileoutputstream fos = new fileoutputstream(filename); objectoutputstream out = new objectoutputstream(fos); out.writeobject(array); out.flush(); out.close(); } catch (ioexception e) { system.out.println(e); } } public float[] loadarray(string filename) { try { fileinputstream fis = new fileinputstream(filename); objectinputstream in = new objectinputstream(fis); float[] saved_array = (float[])in.readobject(); in.close(); return saved_array; } catch (exception e) { system.out.println(e); } return null; }
this return me when try save
05-04 07:21:59.547: i/system.out(622): java.io.filenotfoundexception: /the x: open failed: erofs (read-only file system) 05-04 07:21:59.547: i/system.out(622): java.io.filenotfoundexception: /the x: open failed: enoent (no such file or directory) 05-04 07:21:59.557: i/system.out(622): null
edit2: found solution, want more help!
i left savearray before, , create dir , file earlier.
final file dir = new file(context.getfilesdir() + "/works"); dir.mkdirs(); final file file = new file(dir, "the_x.txt"); savearray(file.getpath()+file.getname(),x);
and seems saved. now, try load on array, new array "a" never change values. think mistake? above loadarray function. try these:
a = loadarray(file.getpath()+file.getname()); = loadarray(file.getname()); = loadarray(file.getpath()); = loadarray("the_x.txt");
if writting file in sd card need following permissions
<uses-permission android:name="android.permission.receive_boot_completed" > </uses-permission> <uses-permission android:name="android.permission.write_external_storage" > </uses-permission>
hence filanem
should soething "/sdcard/yourfile.txt"
you can use thing
use
objectoutputstream objectoutputstream = new objectoutputstream( context.openfileoutput( filename, context.mode_private));
Comments
Post a Comment