Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从单个文件中保存和保存多个对象?_Java_Android_Objectinputstream - Fatal编程技术网

Java 如何从单个文件中保存和保存多个对象?

Java 如何从单个文件中保存和保存多个对象?,java,android,objectinputstream,Java,Android,Objectinputstream,我试图创建一个系统,用户可以在其中选择一组ID(联合ID)从外部API检索数据。从web服务检索数据后,我希望将其存储在本地以供以后使用。 现在,我的问题是我知道如何通过获取整个文件(federations.dat)从ObjectInputStream加载对象。我有没有办法从“federations.dat”加载“object WHERE id=N”?还是必须为每个对象创建单独的文件 这是我的加载方法: public static Object load(Context ctx, String

我试图创建一个系统,用户可以在其中选择一组ID(联合ID)从外部API检索数据。从web服务检索数据后,我希望将其存储在本地以供以后使用。 现在,我的问题是我知道如何通过获取整个文件(federations.dat)从ObjectInputStream加载对象。我有没有办法从“federations.dat”加载“object WHERE id=N”?还是必须为每个对象创建单独的文件

这是我的加载方法:

public static Object load(Context ctx, String filename) throws FileNotFoundException 
{
    Object loadedObj = null;
    InputStream instream = null;

    instream = ctx.openFileInput(filename);

    try {
        ObjectInputStream ois = new ObjectInputStream(instream);
        loadedObj = ois.readObject();
        return loadedObj;

    } catch (StreamCorruptedException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

有什么建议吗?

你可以这样使用它

ArrayList<Object> arrayList = new ArrayList<Object>();

Object obj = null;

while ((obj = ois.readObject()) != null) {
    arrayList.add(obj);
}
public static ArrayList<Object> load(Context ctx, String filename) 
{
    InputStream fis = null;
    ObjectInputStream ois = null;

    ArrayList<Object> arrayList = new ArrayList<Object>();

    Object loadedObj = null;
    try {
        fis = ctx.openFileInput(filename);
        ois = new ObjectInputStream(fis);

        while ((loadedObj = ois.readObject()) != null) {
             arrayList.add(loadedObj);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (null != ois) ois.close();
        if (null != fis) fis.close();
    }

    return arrayList;
}
编辑: 完整代码如下

ArrayList<Object> arrayList = new ArrayList<Object>();

Object obj = null;

while ((obj = ois.readObject()) != null) {
    arrayList.add(obj);
}
public static ArrayList<Object> load(Context ctx, String filename) 
{
    InputStream fis = null;
    ObjectInputStream ois = null;

    ArrayList<Object> arrayList = new ArrayList<Object>();

    Object loadedObj = null;
    try {
        fis = ctx.openFileInput(filename);
        ois = new ObjectInputStream(fis);

        while ((loadedObj = ois.readObject()) != null) {
             arrayList.add(loadedObj);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (null != ois) ois.close();
        if (null != fis) fis.close();
    }

    return arrayList;
}
公共静态ArrayList加载(上下文ctx,字符串文件名)
{
InputStream fis=null;
ObjectInputStream ois=null;
ArrayList ArrayList=新的ArrayList();
对象loadedObj=null;
试一试{
fis=ctx.openFileInput(文件名);
ois=新的ObjectInputStream(fis);
而((loadedObj=ois.readObject())!=null){
arrayList.add(loadedObj);
}
}捕获(例外e){
e、 printStackTrace();
返回null;
}最后{
如果(null!=ois)ois.close();
如果(null!=fis)fis.close();
}
返回数组列表;
}

希望对您有所帮助。

您可以这样使用它

ArrayList<Object> arrayList = new ArrayList<Object>();

Object obj = null;

while ((obj = ois.readObject()) != null) {
    arrayList.add(obj);
}
public static ArrayList<Object> load(Context ctx, String filename) 
{
    InputStream fis = null;
    ObjectInputStream ois = null;

    ArrayList<Object> arrayList = new ArrayList<Object>();

    Object loadedObj = null;
    try {
        fis = ctx.openFileInput(filename);
        ois = new ObjectInputStream(fis);

        while ((loadedObj = ois.readObject()) != null) {
             arrayList.add(loadedObj);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (null != ois) ois.close();
        if (null != fis) fis.close();
    }

    return arrayList;
}
编辑: 完整代码如下

ArrayList<Object> arrayList = new ArrayList<Object>();

Object obj = null;

while ((obj = ois.readObject()) != null) {
    arrayList.add(obj);
}
public static ArrayList<Object> load(Context ctx, String filename) 
{
    InputStream fis = null;
    ObjectInputStream ois = null;

    ArrayList<Object> arrayList = new ArrayList<Object>();

    Object loadedObj = null;
    try {
        fis = ctx.openFileInput(filename);
        ois = new ObjectInputStream(fis);

        while ((loadedObj = ois.readObject()) != null) {
             arrayList.add(loadedObj);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (null != ois) ois.close();
        if (null != fis) fis.close();
    }

    return arrayList;
}
公共静态ArrayList加载(上下文ctx,字符串文件名)
{
InputStream fis=null;
ObjectInputStream ois=null;
ArrayList ArrayList=新的ArrayList();
对象loadedObj=null;
试一试{
fis=ctx.openFileInput(文件名);
ois=新的ObjectInputStream(fis);
而((loadedObj=ois.readObject())!=null){
arrayList.add(loadedObj);
}
}捕获(例外e){
e、 printStackTrace();
返回null;
}最后{
如果(null!=ois)ois.close();
如果(null!=fis)fis.close();
}
返回数组列表;
}

希望它有帮助。

对@Jan代码的扩展,修复了在抛出异常时保持
ois
打开的问题,以及一些小问题

public static ArrayList<Object> load(Context ctx, String filename) throws FileNotFoundException {
    InputStream instream = ctx.openFileInput(filename);

    ArrayList<Object> objects = new ArrayList<Object>();

    try {
        ObjectInputStream ois = new ObjectInputStream(instream);
        try{
            Object loadedObj = null;
            while ((loadedObj = ois.readObject()) != null) {
                objects.add(loadedObj);
            }

            return objects;
        }finally{
            ois.close();
        }

    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
公共静态ArrayList加载(上下文ctx,字符串文件名)引发FileNotFoundException{
InputStream instream=ctx.openFileInput(文件名);
ArrayList对象=新的ArrayList();
试一试{
ObjectInputStream ois=新ObjectInputStream(流内);
试一试{
对象loadedObj=null;
而((loadedObj=ois.readObject())!=null){
添加(loadedObj);
}
归还物品;
}最后{
ois.close();
}
}捕获(StreamCorruptedException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
返回null;
}

对@Jan代码的扩展,修复了在抛出异常时保持
ois
打开的问题,以及一些小问题

public static ArrayList<Object> load(Context ctx, String filename) throws FileNotFoundException {
    InputStream instream = ctx.openFileInput(filename);

    ArrayList<Object> objects = new ArrayList<Object>();

    try {
        ObjectInputStream ois = new ObjectInputStream(instream);
        try{
            Object loadedObj = null;
            while ((loadedObj = ois.readObject()) != null) {
                objects.add(loadedObj);
            }

            return objects;
        }finally{
            ois.close();
        }

    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
公共静态ArrayList加载(上下文ctx,字符串文件名)引发FileNotFoundException{
InputStream instream=ctx.openFileInput(文件名);
ArrayList对象=新的ArrayList();
试一试{
ObjectInputStream ois=新ObjectInputStream(流内);
试一试{
对象loadedObj=null;
而((loadedObj=ois.readObject())!=null){
添加(loadedObj);
}
归还物品;
}最后{
ois.close();
}
}捕获(StreamCorruptedException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
返回null;
}

从android应用程序,您可以从android应用程序保存到it数据库中,如果我没有弄错的话,您可以保存到it数据库中,这会在引发异常时使ois打开。我的OOP类主张使用两个嵌套的尝试,外部的一个捕获异常,内部的一个只有一个finally关闭ois。。我只是对@litemode的代码做了必要的编辑。这是开发商必须解决的问题。但是无论如何,这是一个很好的捕获。而且,第一次从ois读取对象时,它似乎被丢弃。如果我没有弄错的话,当抛出异常时,这将使ois打开。我的OOP类主张使用两个嵌套的尝试,外部的一个捕获异常,内部的一个只有一个finally关闭ois。。我只是对@litemode的代码做了必要的编辑。这是开发商必须解决的问题。但无论如何,这是一个很好的捕获。而且,第一次从ois读取对象时,它似乎被丢弃了。