Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree6Likes

Thread: Binary I/O

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Binary I/O

    We're learning how to use Binary I/O commands...which equates to....

    The dilemma is as follows:

    Binary I/O-screen-shot-2014-04-25-12.55.42-pm.jpg

    My issue is trying to relate the Fraction objects (which we are to create using a loop) with the read/write methods used in Binary I/O (input/output streams). I left a blank after the output.write(), so you can see where the issue exist.
    I'd really appreciate any input.

    Java Code:
    import java.io.*;
    
    public class FractionTest {
    
    	public static void main(String[] args) {
    		int [] fraction = new int[3];
    		
    		for(int i = 0; i <= fraction.length; i++){
    			Fraction numbers = new Fraction();
    		}	
    		
    		try {
    				ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("SerialF.dat"));
    					output.writeObject______________;
    			
    			} catch (FileNotFoundException e1) {	
    				e1.printStackTrace();
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    		
    		try {
    			ObjectInputStream input = new ObjectInputStream(new FileInputStream("SerialF.dat"));
    			
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}			
    }
    Second Class

    Java Code:
    public class Fraction implements java.io.Serializable{
    	private int numerator, denominator;
    	int num, den;
    	private static char slash = '/';
    
    public Fraction(){
    }
    	int getnumerator(){
    		return numerator;
    	}
    	void setnumerator(int numerator){
    		this.numerator = numerator;
    	}
    	
    	int getdenominator(){
    		return denominator;
    	}
    	
    	void setdenominator(int denominator){
    		this.denominator = denominator;
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary I/O

    Just write the object you want to write to the ObjectOutputStream and let that stream do the detailed work; read the API documentation for the class.

    kind regards,

    Jos

    ps. Your Fraction is an Object ...
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

  4. #4
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    We are asked to use a loop to create and write the objects... so should I even be using the fraction array and using a for loop outside ObjectOutputStream? I'm also not really sure what three objects we are creating...?!? Is each object to consists of a numerator and denominator?

    Also, I think the get/set methods we are to use certainly makes it more confusing.

    Thank you!


    *Edit*

    Thanks jashburn, I think this will help to fill in some of the gaps.
    Last edited by javaStooge; 04-25-2014 at 07:36 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary I/O

    You should at leat bookmark this link: Java Platform SE 7 everybody needs that documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    Thank you Jos, it looks like a great sight and I appreciate that...but what is the point in being on a forum if you are just going to share links?

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Binary I/O

    ...but what is the point in being on a forum if you are just going to share links?
    give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime
    Norm, JosAH, kneitzel and 1 others like this.

  8. #8
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O


  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary I/O

    Quote Originally Posted by javaStooge View Post
    Thank you Jos, it looks like a great sight and I appreciate that...but what is the point in being on a forum if you are just going to share links?
    I prevents the forum members from regurgitating what has already been written down and should be read by all programmers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Binary I/O

    Found another tutorial that is quite close to your assignment: Java object serialization - Tutorial (Internet search term used: "java serialization tutorial) although the first (previous) tutorial I posted goes more into the theory behind Java object serialization.

    We are asked to use a loop to create and write the objects... so should I even be using the fraction array and using a for loop outside ObjectOutputStream?
    The way I read it, it simply means use a loop, such as a for loop (so that you can easily control the number of loop iterations), to create a Fraction object and write it into the file, once each iteration.

    I'm also not really sure what three objects we are creating...?!?
    Fraction objects.

    Also, I think the get/set methods we are to use certainly makes it more confusing.
    I think the setters are just so that you can set up the Fraction objects' denominator and numerator before writing them into the file. The getters are so that after reading back the objects from the file, you can check that the objects' denominator and numerator are exactly as were set prior to them being written into the file.

    Is each object to consists of a numerator and denominator?
    Quoting from the first tutorial,
    "In this situation, writeObject traverses the entire web of object references and writes all objects in that web onto the stream."

    In your case a Fraction object contains int and char primitives. When the Fraction object is serialized and written into a file, its data/state (i.e., the primitives) will be automatically written together with the object.

    Jos's point on bookmarking Java Platform SE 7 (i.e., the Java SE API docs) is you can get a lot of relevant information by reading up on the key classes' description and methods. E.g., the key class for you is ObjectOutputStream, which can be found by searching through the lower left-hand side frame on the web page (easier if you use Ctrl-F on your browser.) Quite frequently the documentation in the site is sufficiently clear for you to go ahead with your coding. Note that even pro programmers use the API docs frequently.
    javaStooge likes this.

  11. #11
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Binary I/O

    The important point in my eyes is: Why should we spend a lot of time to write something that someone else wrote down already. This simply leads to errors or discussions.

    So a link to the documentation can be great because that is written with a lot of care and a lot of people read it already so if there was any mistake there is a good chance that it was already fixed.
    And a link to an existing discussion is even pointing out multiple views on a topic which is also quite great.

    I even tend to post google search links. If you could use google already and are used to it, then you might see it as an offense. But it is not an offense! A lot of people are simply not experiences or they simply miss the correct words to put into a search.
    And that often gives a much better list of links than I could provide. Google tries to give you hits in your language. So for example: When I search for "java tutorial" I even get hits in german. I do not know your main language but when you use google, then google knows which provider has the used ip range and in which country it is located. And google even offers translation services. That is also something that I cannot do in here.

    So I hope you understand why these links should be a real help. Maybe you want to describe what help you expected? Object Streams are required to write objects to streams and the linked page gives a lot of details. Writing all that down again would be nonsense or not? Or thinking that we might do a better job (on the fly, quickly while we have time for the forum) than a paid person which was employed for this job?

    With kind regards,

    Konrad

  12. #12
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    @ Konrad, JosAh:

    I apologize if my remark gave you the wrong impression and that I was unappreciative. This could not be further from the truth. I greatly admire and appreciate all that you guys do with your "free" time and I have benefited greatly from it. I'm sure that you see the same questions a lot of the time which probably give you the idea that we have not researched our sources before posting. Generally I spend quite a bit of time trying to find the answer to my questions in the text or online, but when that doesn't work I resort to forums such as this. I think I have become a little frustrated from the repeated speed bumps I hit in this Intro Java course I am taking online, and this forum is my only connection to a knowledge base that can provide some guidance and direction. So when I received the link, I suppose I felt like I was just given a boat without a paddle, set adrift in an ocean of information.

    Kindly.

    @Jashburn, for the Fraction object, my question was regarding whether we are supposed to pass any parameters (e.g. int numerator , int denominator)..?

  13. #13
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    @Jashburn, the first link you provided in the that reply is great, very close to what I am looking for and similar to a couple others I've found online. I understand and follow exactly what they did...this is very much like how our textbook presents it. What I don't understand is how to create three objects from the Fraction class that will write the integer values to ObjectOutputStream..all within a single for loop. At least, this is how I interpret the requirements of our assignment. For me its a coding issue rather than a logic issue.

    ex:
    Java Code:
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("SerialF.dat"));
    for(int i = 0 ; i < 3; i++){
    						Fraction fraction = new Fraction(int numerator, int denominator);
    					}
    Thanks for the help Jashburn.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Binary I/O

    The problem you are facing right now has nothing to do with writing objects. You need to figure out how to create three classes in a loop.
    Do you want to vary the constructor parameters? You could do it with random numbers or an array of denominators and numerators.

    // denoms and nums must be same length arrays.
    Java Code:
    int [] denoms = {   };
    int [] nums = { };
    for (int j = 0; j < denoms.length; j++) {
         Fraction f = new Fraction( /* arguments */ );
         // Test the fraction object to make certain it works.
         // write object when you get the other part working.
    }
    The above is the part of programming which you will never learn in a book. You must think about the tools you have learned and how you might apply them.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    Hey Jim, you are right, that was part of the problem. I think I've written the new code out properly, but the console output is showing the ASCII text.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class FractionTest {
    
    	public static void main(String[] args) throws ClassNotFoundException {
    		int[] numerator = { 2, 3, 4 };
    		int[] denominator = { 8, 6, 4 };
    		
    		try {
    			ObjectOutputStream output = new ObjectOutputStream(
    					new FileOutputStream("SerialF.dat"));
    
    			for (int i = 0; i < denominator.length; i++) {
    				Fraction fraction = new Fraction(numerator, denominator);
    				output.writeObject(fraction);
    			}
    			output.close();
    
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    
    		try {
    			ObjectInputStream input = new ObjectInputStream(
    					new FileInputStream("SerialF.dat"));
    			for (int i = 0; i < denominator.length; i++) {
    				Fraction fraction = (Fraction) (input.readObject());
    				System.out.println("Fraction: " + fraction);
    			}
    			input.close();
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    Binary I/O-screen-shot-2014-04-25-4.53.12-pm.jpg

  16. #16
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Binary I/O

    The problem is, that Java does not know how to print your class. You have to override the toString method in your class if you want to print it.

    Konrad

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Binary I/O

    Th String with the @ (can't copy it from an image) is the value from the Object class's default toString() method:
    ClassName@hashvalue
    javaStooge likes this.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Binary I/O

    In this particular case you may not need to override the toString() method for pretty printing because, quoting from the instructions,
    Add a constructor(s), set and get methods to the Fraction class appropriately.
    So instead of
    Java Code:
    System.out.println("Fraction: " + fraction);
    You can print out fraction's numerator and denominator obtained using the corresponding get methods.

    --- Start edit ---
    Just noticed... you have
    Java Code:
    int[] numerator = { 2, 3, 4 };
    int[] denominator = { 8, 6, 4 };
    ...
    Fraction fraction = new Fraction(numerator, denominator);
    You're passing in int arrays into Fraction?
    --- End edit ---

    Btw, to digress slightly, call your output.close() and input.close() within finally blocks. See The finally Block (The Java™ Tutorials > Essential Classes > Exceptions).
    Last edited by jashburn; 04-25-2014 at 11:29 PM. Reason: Add bit on passing in int arrays

  19. #19
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    Quote Originally Posted by jashburn View Post
    You can print out fraction's numerator and denominator obtained using the corresponding get methods.

    --- Start edit ---
    Just noticed... you have

    You're passing in int arrays into Fraction?
    --- End edit ---
    Do you think this is an unorthodox way of passing values into Fraction? The assignment said we could choose any values and Jim presented a straightforward way of accomplishing that.

    I'm glad you said that the toString was unnecessary, since I was unable to find any examples in the textbook of them doing it that way. I was planning to code the Fraction class with the proper set/get methods, but wanted to test the output (as Jim had suggested) and that is why I tried the System.out.print() command. Unfortunately that didn't work out, another speed bump! YAY!

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Binary I/O

    I use the toString() method a lot for debugging the contents of classes when using the println() statement.
    It takes many fewer keystrokes than using getter methods that might be used by the code as part of the class's definition.

    For example in a linkedlist's node class:
    Java Code:
          public String toString() {            //<<<<<<<< for debug
            return "node nbr="+number + ", next="+next;
          }
    
    //  a sample usage where current is a node with the above method:
          System.out.println("PL current="+current); //<<<<<<<<<
    //PL current=node nbr=0, next=node nbr=1, next=node nbr=2, next=node nbr=3, next=null
    Last edited by Norm; 04-26-2014 at 12:14 AM.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2013, 10:25 PM
  2. Replies: 2
    Last Post: 02-22-2013, 06:10 PM
  3. Replies: 1
    Last Post: 11-01-2011, 05:35 PM
  4. Replies: 5
    Last Post: 08-25-2011, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •