将来の使用のために float 配列をファイルに保存するこのコードがあります。しかし、saveArray("the x",x);
xが保存したいfloat配列である順序で呼び出すと、ファイルにNullが返され、作成されません。
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;
}
これは、保存しようとすると返されるものです
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: 解決策を見つけましたが、さらに助けが必要です!
以前のようにsaveArrayを残して、以前にdirとfileを作成しました。
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);
そして救われたようです。今、別の配列にロードしようとしましたが、新しい配列「a」は値を変更しません。どこが間違っていると思いますか?上記の loadArray 関数を見てください。私はこれらを試します:
a = loadArray(file.getPath()+file.getName());
a = loadArray(file.getName());
a = loadArray(file.getPath());
a = loadArray("the_x.txt");