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 7 of 7

Thread: Problem Using DataStream

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

    Default Problem Using DataStream

    I used DataStream to write some primitive type data to a file, and then read it back. The data was read back and displayed on the console fine, but when i saw the file i was writing to i found lots of special characters- pounds signs and all. Here is the code :
    import java.io.*;
    class Data{
    	static final double[] prices = {12.22,11.11,13.33,14.44,15.55};
    	static final int[] qtys = {1,2,3,4,5};
    	static final String[] descs = {"space shuttle", "titan", "cassini", "black hole", "proxima centauri"};
    	public static void main(String ...arg) throws Exception{
    		DataOutputStream o = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Cosmos.txt")));
    		for(int i=0;i<5;i++){
    			o.writeDouble(prices[i]);
    			o.writeInt(qtys[i]);
    			o.writeUTF(descs[i]);
    		}
    		o.close();
    		DataInputStream di = new DataInputStream(new BufferedInputStream(new FileInputStream("Cosmos.txt")));
    		double price;
    		int qty;
    		String desc;
    		try{
    			while(true){
    				price = di.readDouble();
    				qty = di.readInt();
    				desc = di.readUTF();
    				System.out.format("You Ordered %d units of %s at Rs.%.2f %n", qty, desc, price);			
    				System.out.println();
    			}
    		}catch(EOFException e){
    			System.out.println("Done!!");
    		}
    		di.close();
    	}
    }
    Please help!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Problem Using DataStream

    This should be an expected behavior because you are writing datatypes of different sizes...a text file contains single byte (or 2 byte for unicode) characters. However when you write an int (4 bytes) or double (8 bytes) then the reader tries to read these values as chars (4 char or 8 char, respectively) which often leaves the output looking just as you explain.

  3. The Following User Says Thank You to copeg For This Useful Post:

    rrahulvverma (July 27th, 2010)

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

    Default Re: Problem Using DataStream

    Quote Originally Posted by copeg View Post
    This should be an expected behavior because you are writing datatypes of different sizes...a text file contains single byte (or 2 byte for unicode) characters. However when you write an int (4 bytes) or double (8 bytes) then the reader tries to read these values as chars (4 char or 8 char, respectively) which often leaves the output looking just as you explain.
    But is that not exactly the problem Data Streams are meant to handle?
    I mean if they are capable of outputting primitive type data to a file, but i don't see the data...something just seems amiss. Isn't there a way then, to output such integer type data?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Problem Using DataStream

    i don't see the data
    How are you trying to "see the data"?
    The data in the file is in the same format it has in the computer. Purely bits and bytes. It has not been converted to text characters for human viewing.
    If you want to "see the data" as characters then you need to convert the data to Strings.
    Do a Google on ASCII characters to see the "values" for the viewable characters. For example the char '1' has a decimal value of 49.

  6. The Following User Says Thank You to Norm For This Useful Post:

    rrahulvverma (July 28th, 2010)

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem Using DataStream

    If you want to write out the string representation of a number, you can use a PrintStream or BufferedWriter to write these out.

    DataStream writes out the actual byte values, so while you can easily read back in the value and use it as an int, you will need to analyze either the hex values of the file, or happen to know the character mapping that you're looking at the file with (ASCII, Unicode, etc.), and then compare the values of the characters you see rather than just the characters you see.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    rrahulvverma (July 28th, 2010)

  9. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Reporting spam

    How to report this junk?

  10. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Reporting spam

    Quote Originally Posted by Norm View Post
    How to report this junk?
    Junk removed. There should be a report post icon at the top right of the post itself which should notify the mods