1

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.

3
  • Based on the sample input, name and description are identifiable with their key:value format so if the line does not represent a name and the line does not represent a description, then it's safe to assume the line represents a date. Also, if you can, use LocalDate instead of Date. Commented Oct 4, 2018 at 14:07
  • This is not really clear. Maybe you could add some code to show what you have done so far and what you are trying to achieve Commented Oct 4, 2018 at 14:09
  • If name always comes before description, then a new Meeting instance would be needed whenever a line which represents a name is encountered. Commented Oct 4, 2018 at 14:11

2 Answers 2

1

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;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

try (Stream<String> stream = Files.lines(f)) { List<String> ls = stream.sequential().collect(Collectors.toList()); ls.forEach( c-> System.out.println(c)); }
1) readAllLines - is a java 7 way to read whole contents of a file to memory, since it reads the complete file, it may fail if the file is too large because it can not fit into memory. 2) lines - is a java 8 way to read contents of a file, and it is different from readAllLines in 2 ways: a) it returns a Stream<String> object, b) the output (stream) is lazily populated, meaning, it won't read file upfront. It reads file as you consume the elements of the stream.
0

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)
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.