user1119970
user1119970

Reputation: 199

How to read a file to java lists?

I have a very weird file in my company. It was generated by a program, written by people long back. It contains bunch of properties along with the users who have that properties. It looks like below. I have to read this file line by line and seperate the properties and users into two list. for ex, in the first line all the props should be in one list and the users inside the square brackets should be in another list. count of users changing all the time and thats making my task complicated. can some one suggest me to crack it? I cracking my head from couple of hours instead of it. any hints would be higly appreciable.

Properties,prop1,prop2,prop3,[user1,user2,user3,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,user3,user4,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,user3,user4,user5,],prop4,prop5

Upvotes: 2

Views: 533

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

I would write a simple parser with a state machine where [ switches you to reading users and ] switches you to reading properties.

An alternative is to split the lines two ways.

String line;
while((line = bufferedReader.readLine()) != null) {
    for(String parts: line.split("\\[")) {
        String[] userProps = parts.split("\\)");
        String users = userProps.length==1 ? "" : userProps[0];
        String props = userProps[userProps.length-1];
        // break up the individual users and props
    }
}

Upvotes: 2

Related Questions