T
T
thatmaniscool2018-07-06 22:49:12
Java
thatmaniscool, 2018-07-06 22:49:12

How to write java class to dat file?

Let's say I have a USER class

public class User {
  private String name;
  private int age;
  
  public User (String name, int age) {
    this.name = name;
    this.age = age;
  }
  
  public String toString () {
    return "Name: " + name + " age: " + age;
  }
}

The main class has a list from the USER class, I need to write data to a dat file and, if necessary, read and restore the list.
public class Main {
  public static void main (String [] args) {
    List <User> UserList = new ArrayList <> ();
    UserList.add(new User ("Example", 1));
    UserList.add(new User ("Example", 2));
    UserList.add(new User ("Example", 3));
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2018-07-07
@zagayevskiy

Google serialization. You can use Serializable, or json, xml. A million options.

S
Sergey Gornostaev, 2018-07-07
@sergey-gornostaev

The question still remains: Why would someone help you if you don't accept answers?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question