Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.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 从字符串读取对象时发生StreamCorruptedException_Java_Android_Serialization - Fatal编程技术网

Java 从字符串读取对象时发生StreamCorruptedException

Java 从字符串读取对象时发生StreamCorruptedException,java,android,serialization,Java,Android,Serialization,这是我的密码: try { Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv"); File file = new File(uri.getPath()); //Read the file BufferedReader br = new BufferedReader(ne

这是我的密码:

 try {

                Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv");
                File file = new File(uri.getPath());
                //Read the file
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                boolean firstLine = true;

                while ((line = br.readLine()) != null) {
                    System.out.println("Print the contents from the file :" + line);
                    if (firstLine) {
                        firstLine = false;
                        continue;
                    } else {

                            //deserialize the string to Object
                            td = TransferData.fromString(line); //Where td is an object 

                            //deserialize the string to Object
                            System.out.println("deserialized: " + td);



               }
但是,我在这一行遇到了一个例外:

td = TransferData.fromString(line);
从我的fromString函数:

/** Read the object from Base64 string. */
    public static TransferData fromString( String s ) throws IOException, ClassNotFoundException     {
        byte [] data = Base64.decode(s, Base64.DEFAULT);
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(  data ) );
        Object o  = ois.readObject();
        ois.close();
        return (TransferData)o;
    }
例外是StreamCorruptedException,但我不确定为什么会这样。我希望能够读入字符串并反序列化该字符串

编辑:

  /** Write the object to a Base64 string. */
    public static String toString(Serializable o) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(output);
        oos.writeObject(o);
        oos.close();
        return new String(Base64.encode(output.toByteArray(), Base64.DEFAULT));
    }



//SerialVersionID
  private static final int serialVersionUID = 10032014;

我可以看到您的代码中存在一些缺陷:

  • 每次读取新行()
  • 如果您是通过
    BufferedReader
    读取文件,则不需要
    ObjectInputStream
    和反之亦然
  • 我不明白,你为什么要对字符串进行
    Base64.decode
  • 你必须保证这是连贯的
  • 如果我理解得很好,您希望将对象序列化为文件,然后反序列化它。首先,您应该有一个实现接口的实体(对象):

    public class Foo implements Serializable{
        static final long serialVersionUID = 42L;
        //class code...
    }
    
    现在,您可以安全地将对象(或对象列表)保存到文件中:

    FileOutputStream fos = new FileOutputStream("myFoo.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    Foo foo = new Foo();
    //Fill the object.
    
    oos.writeObject(foo);
    
    如果您需要读取该文件,现在可以执行以下操作:

    FileInputStream fis = new FileInputStream("myFoo.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    Foo mySavedFoo = (Foo) ois.readObject();
    
    如果需要保存对象列表,可以使用实现可序列化的列表:

    FileOutputStream fos = new FileOutputStream("myFoos.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    List<Foo> foos = new ArrayList<Foo>();
    //Fill the list.
    
    oos.writeObject(foos);
    
    //...
    
    FileInputStream fis = new FileInputStream("myFoos.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    ArrayList<Foo> mySavedFoo = (ArrayList<Foo>) ois.readObject();
    
    就像我前面的示例一样,您通过
    ObjectOutputStream
    (顺便说一下?)存储
    Serializable
    对象,但是
    Serializable
    只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:

    public static String toString(Foo o) throws IOException { //...
    

    Base64.encode
    是在对象保存到文件后完成的!!没有“加密”!此外,对
    输出
    对象进行编码,该对象是由tearrayoutputstream生成的,与
    o
    中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:

  • 每次读取新行()
  • 如果您是通过
    BufferedReader
    读取文件,则不需要
    ObjectInputStream
    和反之亦然
  • 我不明白,你为什么要对字符串进行
    Base64.decode
  • 你必须保证这是连贯的
  • 如果我理解得很好,您希望将对象序列化为文件,然后反序列化它。首先,您应该有一个实现接口的实体(对象):

    public class Foo implements Serializable{
        static final long serialVersionUID = 42L;
        //class code...
    }
    
    现在,您可以安全地将对象(或对象列表)保存到文件中:

    FileOutputStream fos = new FileOutputStream("myFoo.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    Foo foo = new Foo();
    //Fill the object.
    
    oos.writeObject(foo);
    
    如果您需要读取该文件,现在可以执行以下操作:

    FileInputStream fis = new FileInputStream("myFoo.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    Foo mySavedFoo = (Foo) ois.readObject();
    
    如果需要保存对象列表,可以使用实现可序列化的列表:

    FileOutputStream fos = new FileOutputStream("myFoos.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    List<Foo> foos = new ArrayList<Foo>();
    //Fill the list.
    
    oos.writeObject(foos);
    
    //...
    
    FileInputStream fis = new FileInputStream("myFoos.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    ArrayList<Foo> mySavedFoo = (ArrayList<Foo>) ois.readObject();
    
    就像我前面的示例一样,您通过
    ObjectOutputStream
    (顺便说一下?)存储
    Serializable
    对象,但是
    Serializable
    只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:

    public static String toString(Foo o) throws IOException { //...
    

    Base64.encode
    是在对象保存到文件后完成的!!没有“加密”!此外,对
    输出
    对象进行编码,该对象是由tearrayoutputstream生成的,与
    o
    中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:

  • 每次读取新行()
  • 如果您是通过
    BufferedReader
    读取文件,则不需要
    ObjectInputStream
    和反之亦然
  • 我不明白,你为什么要对字符串进行
    Base64.decode
  • 你必须保证这是连贯的
  • 如果我理解得很好,您希望将对象序列化为文件,然后反序列化它。首先,您应该有一个实现接口的实体(对象):

    public class Foo implements Serializable{
        static final long serialVersionUID = 42L;
        //class code...
    }
    
    现在,您可以安全地将对象(或对象列表)保存到文件中:

    FileOutputStream fos = new FileOutputStream("myFoo.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    Foo foo = new Foo();
    //Fill the object.
    
    oos.writeObject(foo);
    
    如果您需要读取该文件,现在可以执行以下操作:

    FileInputStream fis = new FileInputStream("myFoo.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    Foo mySavedFoo = (Foo) ois.readObject();
    
    如果需要保存对象列表,可以使用实现可序列化的列表:

    FileOutputStream fos = new FileOutputStream("myFoos.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    List<Foo> foos = new ArrayList<Foo>();
    //Fill the list.
    
    oos.writeObject(foos);
    
    //...
    
    FileInputStream fis = new FileInputStream("myFoos.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    ArrayList<Foo> mySavedFoo = (ArrayList<Foo>) ois.readObject();
    
    就像我前面的示例一样,您通过
    ObjectOutputStream
    (顺便说一下?)存储
    Serializable
    对象,但是
    Serializable
    只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:

    public static String toString(Foo o) throws IOException { //...
    

    Base64.encode
    是在对象保存到文件后完成的!!没有“加密”!此外,对
    输出
    对象进行编码,该对象是由tearrayoutputstream生成的,与
    o
    中存储的真实数据无关。我可以看到您的代码中存在一些缺陷:

  • 每次读取新行()
  • 如果您是通过
    BufferedReader
    读取文件,则不需要
    ObjectInputStream
    和反之亦然
  • 我不明白,你为什么要对字符串进行
    Base64.decode
  • 你必须保证这是连贯的
  • 如果我理解得很好,您希望将对象序列化为文件,然后反序列化它。首先,您应该有一个实现接口的实体(对象):

    public class Foo implements Serializable{
        static final long serialVersionUID = 42L;
        //class code...
    }
    
    现在,您可以安全地将对象(或对象列表)保存到文件中:

    FileOutputStream fos = new FileOutputStream("myFoo.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    Foo foo = new Foo();
    //Fill the object.
    
    oos.writeObject(foo);
    
    如果您需要读取该文件,现在可以执行以下操作:

    FileInputStream fis = new FileInputStream("myFoo.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    Foo mySavedFoo = (Foo) ois.readObject();
    
    如果需要保存对象列表,可以使用实现可序列化的列表:

    FileOutputStream fos = new FileOutputStream("myFoos.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    
    List<Foo> foos = new ArrayList<Foo>();
    //Fill the list.
    
    oos.writeObject(foos);
    
    //...
    
    FileInputStream fis = new FileInputStream("myFoos.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    
    ArrayList<Foo> mySavedFoo = (ArrayList<Foo>) ois.readObject();
    
    就像我前面的示例一样,您通过
    ObjectOutputStream
    (顺便说一下?)存储
    Serializable
    对象,但是
    Serializable
    只是一个接口。如果使用实现它的类,效果会更好。按照我的例子,它将变成:

    public static String toString(Foo o) throws IOException { //...
    
    Base64.encode
    是在对象保存到文件后完成的!!没有“加密”!此外,编码是对
    输出
    对象进行的,该对象是由tearrayoutputstream生成的,无需任何操作