Hi. I am actually a C/C++ programmer but is learning JAVA now. How can I store the data of a file that I read line by line into a structure or class?
Thank you.
Printable View
Hi. I am actually a C/C++ programmer but is learning JAVA now. How can I store the data of a file that I read line by line into a structure or class?
Thank you.
Example:
Code :class Student{ private int roll; private String name; public void setRoll(int roll){ this.roll = roll; } public void setName(String name){ this.name = name; } }
Code :class ReadFile{ public static void main(String args[]){ Student s = new Student(); //File reading code; //read Roll from file s.setRoll(rollno read from file); //read Name from file s.setName(name read from file); [B]//now you can use s as structure or class object having student info[/B] } }
Thanks Dan. My problem is that the Student Name, Surname, Birth Day is in one line that you read from the file with constant field size's. Can one not read it directly into the struct from the file?
Ok, I will use ByteBuffer.get to read the line buffer.
You will have to parse the line. First, create a class as DanBrown outlined above that corresponds to the data in the line, get the line, then you can parse the line depending upon the delim (either manually, or using something like StringTokenizer or String.split), which will give you the individual data elements that you can then populate the class (or in C++ terms the struct) with.Quote:
Can one not read it directly into the struct from the file?