0

我正在尝试使用此方法从 java 中的文件中读取写入,我已经覆盖了 Externalizable 我如何使用的方法,但使用了某种分隔符,例如“;” at string 用于了解带有空格的字符串有多长。

    public class Person extends Om {

        private String job;
        private ArrayList<Integer> hobbiesIDs;
        private String birthDate;

    @Override
    public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
        this.setId(Integer.parseInt(in.readUTF()));
        this.setFirstNname(in.readUTF());
        this.setLastName(in.readUTF());
        this.setBirthDate(in.readUTF());
        this.setJob(in.readUTF());

    }
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(Integer.toString(this.getId()));
        out.writeUTF(this.getFirstNname());
        out.writeUTF(this.getLastName());
        out.writeUTF(this.getBirthDate());
        out.writeUTF(this.getJob());
    }
    }

    @SuppressWarnings("unchecked")
    public void readFile() {
            ArrayList<Person
> tempPersons = new ArrayList<>()

        try {
            InputStream inputStream = new FileInputStream("Persoane.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

            tempPersons = (ArrayList<Person>) objectInputStream.readObject();
            persons = (ArrayList<Person>) tempPersons.clone();

            objectInputStream.close();
            inputStream.close();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("exception: " + e);
        }

    public void writeFile() {
        try {
            OutputStream outputStream = new FileOutputStream("Persoane.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

            objectOutputStream.writeObject(persons);
            objectOutputStream.flush();
            outputStream.flush();

            objectOutputStream.close();
            outputStream.close();

        } catch (IOException e) {
            System.out.println("exception: " + e);
        }
4

0 回答 0