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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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:
class Student{ private int roll; private String name; public void setRoll(int roll){ this.roll = roll; } public void setName(String name){ this.name = name; } }
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.Can one not read it directly into the struct from the file?