简体   繁体   English

如何使用Java中的readObject()显示文件中的内容?

[英]How can I display the content from a file using readObject() in java?

class Biodata2 implements Serializable {
  String name;
  int age;
  String addr1;
  String addr2;

  Biodata2() {}

  Biodata2(String name,int age,String addr1,String addr2) {
    this.name=name;
    this.age=age;
    this.addr1=addr1;
    this.addr2=addr2;        
  }

  void printdata() {
    System.out.println("Name: "+name);
    System.out.println("Age: "+age);
    System.out.println("Address1: "+addr1);
    System.out.println("Address2: "+addr2);
  }
}

public class ObjectInputStreamBiodata {
  public static void main(String[] args) {
    Biodata2 obj;
    try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/akh/Objectout1.txt"));

      obj=(Biodata2)ois.readObject();
      obj.printdata();
    } catch(Exception e) {
      System.out.println(e);
    }
  }

I'm trying to read some data from a file using readObject (), but didn't display the data. 我正在尝试使用readObject()从文件中读取一些数据,但未显示数据。 Why? 为什么? I also tried to convert the type. 我也尝试转换类型。 But when i compiled the code it shows an excepton-ClassCastException 但是当我编译代码时,它显示了exception-ClassCastException

How can I display the content from a file using readObject()? 如何使用readObject()显示文件中的内容?

首先,必须使用ObjectOutputStream的writeObject(...)方法将要读取的对象写入该文件。

you can try this 你可以试试这个

 FileInputStream fis = new FileInputStream("D:/akh/Objectout1.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
      int i = ois.readInt();
      String s = (String) ois.readObject();

      ois.close();

As I commented need to write (serialize) the object before read (deserialize). 正如我所评论的,需要在读取(反序列化)之前先写(序列化)对象。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Biodata2 implements Serializable {

    String name;
    int age;
    String addr1;
    String addr2;

    Biodata2() {
    }

    Biodata2(String name, int age, String addr1, String addr2) {
        this.name = name;
        this.age = age;
        this.addr1 = addr1;
        this.addr2 = addr2;
    }

    void printdata() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Address1: " + addr1);
        System.out.println("Address2: " + addr2);
    }
}

public class ObjectInputStreamBiodata {

    public static void main(String[] args) {
        Biodata2 w = new Biodata2("sunil", 23, "add1", "add2");
        Biodata2 r;
        try {
            ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("Objectout1.txt"));
            oo.writeObject(w);
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Objectout1.txt"));

            r = (Biodata2) ois.readObject();
            r.printdata();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println(e);
        }
    }
}

Above code write code in file and then read it again. 上面的代码在文件中写入代码,然后再次读取。

Output is like : 输出就像:

Name: sunil
Age: 23
Address1: add1
Address2: add2

Imp points : 展示点:

It is good to close stream. 最好关闭流。 Because close method of almost all the OutputStream classes are flush the data before close. 因为几乎所有OutputStream类的close方法都在关闭之前刷新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM