简体   繁体   English

如何从二进制文件中的对象读取到Java中的ArrayList?

[英]How to read objects from binary file to ArrayList in java?

I'm Java beginner. 我是Java初学者。 I have to write some objects in binary file and than be able to write next object at the end of file and in second case in chronogical order. 我必须在二进制文件中写入一些对象,然后才能按时间顺序在文件末尾和第二种情况下写入下一个对象。 My code right is looks like this: 我的代码正确如下:

class Czas implements Serializable {
    private int rok;
    private int miesiac;
    private int dzien;
    private int godzina;
    private int minuta;

    public Czas(int rok, int miesiac, int dzien, int godzina, int minuta) {
        this.rok = rok;
        this.miesiac = miesiac;
        this.dzien = dzien;
        this.godzina = godzina;
        this.minuta = minuta;
    }

    public int getRok() {
        return rok;
    }

    public int getMiesiac() {
        return miesiac;
    }

    public int getDzien() {
        return dzien;
    }

    public int getGodzina() {
        return godzina;
    }

    public int getMinuta() {
        return minuta;
    }
    }

    class Pomiar implements Serializable /*,Comparable<Pomiar>*/ {
    private Czas czas;
    private double temperatura;

    public Pomiar (Czas czas, double temperatura) {
        this.czas = czas;
        this.temperatura = temperatura;
    }

    public void Zapisz(ObjectOutputStream fout) throws IOException{
        fout.writeObject(this);
    }

    public void ZapisChrono(ObjectOutputStream fout) throws IOException {

    }


    public String toString() {
        return "Temperatura: " + temperatura;
    }
    }




    public class AlgorytmyZ3V3 {

    public static void main(String[] args) {
         try (ObjectOutputStream fout =  new ObjectOutputStream(new FileOutputStream(
                  "BazaPomiarow.dat")))
         {

      Czas czas1 = new Czas(1988, 6, 15, 10, 18);
      Czas czas2 = new Czas(1980, 1, 28, 15, 20);
      Czas czas3 = new Czas(1980, 12, 9, 3, 4);
      Pomiar p1 = new Pomiar(czas1, 30);
      Pomiar p2 = new Pomiar(czas2, 20);
      Pomiar p3 = new Pomiar(czas3, 30);
      p1.Zapisz(fout);
      p2.Zapisz(fout);
      p3.Zapisz(fout);
         }

      catch (IOException e) {
            System.out.println("Błąd wejścia/wyjścia.");
        }
       try {
        ObjectInputStream fin = new ObjectInputStream(new          FileInputStream("BazaPomiarow.dat"));
            ArrayList<Pomiar> lista = new ArrayList();
        }
        catch(Exception e) {
            System.out.println("Wyjątek w czasie deserializacji: " + e);
        }
     }

 }

So I can write next object using method Zapisz, and it will be written at the end of file. 因此,我可以使用Zapisz方法写入下一个对象,该对象将被写入文件末尾。 But I have problem with chronogical order. 但是我对时间顺序有疑问。 I would like to read records from file to ArrayList and then sort that list and add next object with specific index and at last write whole ArrayList again to file. 我想从文件读取记录到ArrayList,然后对该列表进行排序,并添加具有特定索引的下一个对象,最后将整个ArrayList重新写入文件。 That's how I would like to do this and that's why I need to know how to read those objects from file and write to ArrayList. 这就是我想要的方式,这就是为什么我需要知道如何从文件中读取那些对象并将其写入ArrayList的原因。 Maybe it's not the best way, but it's school task and I need to be as acurate as it's possible. 也许这不是最好的方法,但这是学校的工作,我需要尽可能地精通。

sry for names in Polish, but I think it's not a problem here as I only need to know, how to use specific thing in Java. 希望用波兰语命名,但是我认为这不是问题,因为我只需要知道如何在Java中使用特定内容即可。

As this tutorial shows http://www.tutorialspoint.com/java/java_serialization.htm to get objects form serialized file tou need to open file and create new object input stream 如本教程所示, http: //www.tutorialspoint.com/java/java_serialization.htm从序列化文件中获取对象需要打开文件并创建新的对象输入流

FileInputStream fileIn = new FileInputStream("/tmp/myclass.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);

read them as long as it is possible using 阅读它们,只要有可能使用

e = (Employee) in.readObject();

and a loop. 和一个循环。 As for comparision and sorting just look here How to use Comparator in Java to sort 至于比较和排序,请看这里如何在Java中使用Comparator进行排序

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

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