How to create objects from a text file in Java?

0

Hello. I have a class in my project (Person) that contains attributes (for example, name, age, gender, etc) and I have an array of these objects (Person). I also have a text file and inside it is typed several characteristics in a format Name # Age # Sex, for example Ana # 20 # Female in a row, Paulo # 31 # Male in the next. I would like to know if there is a possibility when the program will read this txt file, identify that there is a String before the # character and thus write it to a variable. So I could create an object with the characteristics of the given file (like this: Person p = new Person ("Ana", "20", "Female"). my array.

    
asked by anonymous 18.09.2014 / 22:27

1 answer

0

See how to read text file in Java:

Reading data from Txt with Java

Then, for each row, use the linha.split("#") method to get an array with the value of each attribute:

String atributosPessoa [ ] = line.split(“#”);
    
19.09.2014 / 16:33