我有以下代码并尝试将数组存储在.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 exeption,为什么?我不能在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>
答案 0 :(得分:1)
更改saveChanges()
方法,将Context
作为参数...
public void saveChanges(Context context) {...}
然后在saveChanges(...)
尝试更改此行...
FileOutputStream f_out = new FileOutputStream("myobject.data");
...到...
FileOutputStream f_out = context.openFileOutput("myobject.data", Context.MODE_PRIVATE);
同时将Context
传递给loadBooks(...)
并更改此行...
FileInputStream f_in = new FileInputStream("myobject.data");
...到...
FileInputStream f_in = context.openFileInput("myobject.data");
从Activity
调用任一方法时,您会使用this
作为上下文。实施例...
myBookManager.saveChanges(this);
myBookManager.loadBooks(this);