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.

Results 1 to 5 of 5

Thread: Reading file Data into a "struct"

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading file Data into a "struct"

    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.


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Reading file Data into a "struct"

    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 and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading file Data into a "struct"

    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?

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading file Data into a "struct"

    Ok, I will use ByteBuffer.get to read the line buffer.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading file Data into a "struct"

    Can one not read it directly into the struct from the file?
    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.

Similar Threads

  1. Suddenly getting "Too Many Open File" error
    By newbie14 in forum Java Networking
    Replies: 4
    Last Post: December 12th, 2010, 11:12 PM
  2. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  3. Unable to run "Inspect" on my scrabjob.jpage file
    By GunnDawg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 4th, 2010, 10:07 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM