G
G
gleendo2017-01-20 01:34:00
Java
gleendo, 2017-01-20 01:34:00

How can you read certain parts of lines from a file?

There is an adapter class for Writer:

class EntityOutputWriter implements EntityOutput {
    private final Writer out;

    public EntityOutputWriter(Writer out) {
        this.out = out;
    }

    @Override
    public void writePerson(Person person) throws IOException {
        int age = person.getAge();
        String name = person.getName();

        out.write("<person>\n");
        out.write("    <age>" + age + "</age>\n");
        out.write("    <name>" + name + "</name>\n");
        out.write("</person>\n");

        out.flush();
    }

    @Override
    public void writePoint(Point point) throws IOException {
        out.write("<point x = '" + point.getX() + "' y = '" + point.getY() + "'/>\n");

        out.flush();
    }
}

After using this class, the following data is written to the file (The values ​​are given as an example. In reality, they are specified when writing to the file in the constructor):
<point x = '11' y = '15'/>
<person>
    <age>28</age>
    <name>Nick</name>
</person>

You need to write an adapter for Reader whose read methods will return Point and Person objects:
class EntityInputReader implements EntityInput {
    private final Reader src;

    public EntityInputReader(Reader src) {
        this.src = src;
    }

    @Override
    public Person readPerson() throws IOException {
        // some code
       // return new Person(???, ???);
    }

    @Override
    public Point readPoint() throws IOException {
        // some code
       // return new Point(???, ???);
    }
}

How to write these read methods? How to read only the data: 11, 15, "Nick", 28, so that they can be passed to the constructor in the readPoint() and readPerson() methods?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question