JaneDoe
JaneDoe

Reputation: 13

JAVA8, parsing text file and parsing its values to objects

I have specific text file looking like this:

name: meeting_name1
description: 
04/18/2012 00:00:00
05/18/2012 00:00:00
... (more dates)
07/18/2012 00:00:00
name: meeting_name2
description: some_desc
04/18/2012 00:00:00
05/18/2012 00:00:00
... (more dates)
07/18/2012 00:00:00
(etc)

I have java object looking like this:

class Meeting {
    String name;
    String description;
    List<Date> dates;
}

My point is to read the file, parse values, create objects and save them to database.

I can read the file line by line and convert it to List<String>, ie. all data together. `I can make and fill one java object with values and save it to database.

My issue here is how to find out that I'm at the end of dates and lines (name: meeting_name2) of new object begin.

So I could make something like List<List<String>> where List<String> would be equal to one object, ie. List<Meeting>?

Not sure if its understandable, sorry for formatting.

Upvotes: 1

Views: 2603

Answers (2)

Cường Lư Quốc
Cường Lư Quốc

Reputation: 338

Assumption that you could read the file data to List variable. (See above answer)

List<String> lines = Files.readAllLines(Paths.get("FILE_NAME"));

Now, you can see below code as a demo. It is the simple loop and if else statament. Hope it will help you.

public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        List<String> data = new ArrayList<>();
        data.add("name: meeting_name1");
        data.add("description: some_desc");
        data.add("07/18/2012 00:00:00");
        data.add("07/18/2012 00:00:00");
        data.add("name: meeting_name2");
        data.add("description: some_desc");
        data.add("07/18/2012 00:00:00");

        List<Meeting> result = new ArrayList<>();
        Meeting temp = null;
        for (String line : data) {
            if (line.startsWith("name:")) {
                temp = new Meeting(line.split(":")[1].trim());
                result.add(temp);
            } else if (line.startsWith("description:")) {
                temp.setDescription(line.split(":")[1].trim());
            } else {
                temp.getDates().add(simpleDateFormat.parse(line)); // Use date for
            }
        }
        System.out.println(result.get(0).getName() + ": " + result.get(0).getDates().size()); // meeting_name1: 2
        System.out.println(result.get(1).getName() + ": " + result.get(1).getDates().size()); // meeting_name2: 1    
    }

    static class Meeting {
        String name;
        String description;
        List<Date> dates;

        public String getName() {
            return name;
        }

        public List<Date> getDates() {
            return dates;
        }

        Meeting(String name) {
            this.name = name;
            this.dates = new ArrayList<>();
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

Upvotes: 1

Axel M
Axel M

Reputation: 371

One possibility would be to read all lines first. You would not need to worry about the end of lines with:

List<String> lines = Files.readAllLines(Paths.get("FILE_NAME"));

then iterarate through the array, if a line starts with "name:" you make a new object and add the data like that:

        List<Meeting> meetings = new ArrayList();
        Meeting currentMeeting;

        for (String line : lines) {
            if(line.startsWith("name:"))
            {
                currentMeeting = new Meeting();
                meetings.add(currentMeeting);
                //...add data (name)
            }
            //...add more data (description and dates)
        }

Upvotes: 0

Related Questions