2

我有以下代码并尝试将数组存储在 .data 文件中。问题是我不知道如何执行此操作,尝试创建文件时出现 filenotfoundException。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class SimpleBookManager implements BookManager, java.io.Serializable {

    private ArrayList<Book> allBooks = new ArrayList<Book>();


    public ArrayList<Book> getAllBooks(){
        return  allBooks;
    }

    public void saveChanges(){
        try{
            FileOutputStream f_out = new FileOutputStream("myobject.data");
            // Write object with ObjectOutputStream
            ObjectOutputStream obj_out = new
                ObjectOutputStream (f_out);

            // Write object out to disk
            obj_out.writeObject(allBooks);
        }catch (FileNotFoundException e) {
            System.out.println("No file found saveChanges");//Why can't I create a new "myObject.data"?
        } catch (IOException e) {
            //someCode();
        } 
    }

    public void loadBooks(){
        try{
        // Read from disk using FileInputStream
        FileInputStream f_in = new 
            FileInputStream("myobject.data");

        // Read object using ObjectInputStream
        ObjectInputStream obj_in = 
            new ObjectInputStream (f_in);

        // Read an object
        Object obj = obj_in.readObject();

        if (obj instanceof ArrayList<?>)
        {
            // Cast object to a Vector
            allBooks = (ArrayList<Book>) obj;
            System.out.println("Worked");

        }
        }catch (FileNotFoundException e) {
            System.out.println("No file found LoadBooks");
        } catch (IOException e) {
            //someCode();
        } 
        catch (ClassNotFoundException e) {
            //someCode();
        }

    }


      public static SimpleBookManager getSimpleBookManager()
      {
        if (ref == null){
            ref = new SimpleBookManager();
        }

        return ref;
      }

    public SimpleBookManager(){

    }
}

问题是当我尝试创建它并稍后读取它时,它都会抛出 filenotfound 异常,为什么?我不能在 android 模拟器上创建 .data 文件吗? _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ _ __ _ ___

编辑这是我的清单并<uses permission>在文件末尾添加了,但它仍然无法创建文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.chalmers.BookApp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"

        >
        <activity
            android:name=".BookApp"
            android:label="@string/title_bookapp"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity
        android:name=".AddBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".DetailActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".EditBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>

    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>
4

1 回答 1

1

更改saveChanges()方法以将 aContext作为参数...

public void saveChanges(Context context) {...}

然后saveChanges(...)尝试更改此行...

FileOutputStream f_out = new FileOutputStream("myobject.data");

...到...

FileOutputStream f_out = context.openFileOutput("myobject.data", Context.MODE_PRIVATE);

还传递一个ContexttoloadBooks(...)并更改此行...

FileInputStream f_in = new FileInputStream("myobject.data");

...到...

FileInputStream f_in = context.openFileInput("myobject.data");

从 an 调用任一方法时,Activity您将this用作上下文。例子...

myBookManager.saveChanges(this);
myBookManager.loadBooks(this);
于 2012-11-17T21:08:49.663 回答