Android的Java的 - 反序列化的Andr​​oid平台上的文件文件、序列化、平台上、Android

由网友(弱点是你)分享简介:我有一个序列化存储和读取后文件的java的程序。所以,我把序列化的文件,并尝试读取他们在我的Andr​​oid手机上使用完全相同的code我用在Java SE(在Eclipse工作):的FileInputStream FIS = NULL;尝试 {FIS =新的FileInputStream(iwastedahalf...

我有一个序列化存储和读取后文件的java的程序。所以,我把序列化的文件,并尝试读取他们在我的Andr​​oid手机上使用完全相同的code我用在Java SE(在Eclipse工作):

 
的FileInputStream FIS = NULL;
尝试 {
    FIS =新的FileInputStream(iwastedahalfhouronthis.ser);
   }赶上(FileNotFoundException异常前){
}
 

FileNotFoundException异常抛出。这样就OK了,它可能不是在正确的地方。所以,我把文件中的Eclipse项目中的每一个可能的文件夹,并尝试加载方式,从这些位置。没有运气。确保文件名是正确的,没有运气。使用由日食给出的全名:/pleasehelp/src/com/imlosingit/iwastedahalfhouronthis.ser。没有运气。通过一个文件对象进入的FileInputStream。没有运气。这使Java的很轻松。这是怎么回事吗?

-----------编辑解决方案--------------------

 
        数据数据= NULL; //被反序列化对象
        InputStream的是= NULL;
        ObjectInputStream的OIS = NULL;
        AssetManager资产= getAssets();
        是= assets.open(fff.ser);
        OIS =新的ObjectInputStream(是);
        数据=(com.data.Data)ois.readObject();
 

解决方案

检查出一些干将中的 上下文 。尝试保存文件到高速缓存文件夹,或者如果他们将是较大的文件,你可能会想尝试外部目录(如SD卡)。

 文件DIR = getCacheDir();
//做的东西,将文件保存在这个目录下

的FileInputStream FIS = NULL;
尝试 {
    FIS =新的FileInputStream(新建文件(目录,iwastedhalfhouronthis.ser));
}赶上(FileNotFoundException异常前){
}
 

编辑:看了您的评论后,我建议你存储在您的序列化的文件/资产。然后你可以使用AssetManager检索这些:

  AssetManager资产= getAssets();
AssetFileDescriptor AFD = assets.openFd(iwastedhalfhouronthis.ser);
的FileInputStream FIS = afd.createInputStream();
 

编辑:一件事来尝试。把.ser文件中的一个名为新文件夹 /生(你必须手动创建它),并尝试这样的:

 资源资源= getResources();
//可能需要R.raw.iwastedhalfhouronthis.ser但我不这么认为。
AssetFileDescriptor AFD = res.openRawResourceFd(R.raw.iwastedhalfhouronthis);
的FileInputStream FIS = afd.createInputStream();
 

I have a jave program that serializes files that are stored and read later. So I take the serialized files and try to read them in on my Android phone (working in Eclipse) using the exact same code I used in Java SE:


FileInputStream fis = null;
try {
    fis = new FileInputStream("iwastedahalfhouronthis.ser");
   } catch (FileNotFoundException ex) {
}

FileNotFoundException thrown. So ok, its probably not in the right place. So I place the file in every possible folder within the Eclipse project and try loading from those locations. No luck. Make sure the file name is correct, no luck. Use the fully qualified name given by eclipse: "/pleasehelp/src/com/imlosingit/iwastedahalfhouronthis.ser". No luck. Passed a file object into the FileInputStream. No luck. This was so trivially easy on Java. What's going on here?

-----------EDIT SOLUTION--------------------


        Data data = null; //object to be deserialized
        InputStream is = null;
        ObjectInputStream ois=null;
        AssetManager assets = getAssets();
        is = assets.open("fff.ser");
        ois = new ObjectInputStream(is);
        data = (com.data.Data) ois.readObject();

解决方案

Check out some of the getters in Context. Try saving the files to the cache folder, or if they're going to be large files, you may want to try an external directory (e.g. SD Card).

File dir = getCacheDir();
//do stuff, saving the file in this directory

FileInputStream fis = null;
try {
    fis = new FileInputStream(new File(dir, "iwastedhalfhouronthis.ser"));
} catch (FileNotFoundException ex) {
}

EDIT: After reading your comment, I'd suggest storing your serialized files under /assets. Then you can use AssetManager to retrieve these:

AssetManager assets = getAssets();
AssetFileDescriptor afd = assets.openFd("iwastedhalfhouronthis.ser");
FileInputStream fis = afd.createInputStream();

EDIT: One more thing to try. Put the .ser file in a new folder called /raw (you'll have to create it manually), and try this:

Resources res = getResources();
//might need "R.raw.iwastedhalfhouronthis.ser" but I don't think so.
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.iwastedhalfhouronthis);
FileInputStream fis = afd.createInputStream();

阅读全文

相关推荐

最新文章