Go Back   Java Programming Forums > Java Standard Edition Programming Help > File I/O & Other I/O Streams


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27-07-2010, 03:51 PM
Junior Member
 

Join Date: Jan 2010
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
rrahulvverma is on a distinguished road
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 :
Java 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!



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 27-07-2010, 04:09 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 677
Thanks: 8
Thanked 149 Times in 141 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
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.
Reply With Quote
The Following User Says Thank You to copeg For This Useful Post:
rrahulvverma (27-07-2010)
  #3 (permalink)  
Old 27-07-2010, 05:47 PM
Junior Member
 

Join Date: Jan 2010
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
rrahulvverma is on a distinguished road
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?
Reply With Quote
  #4 (permalink)  
Old 27-07-2010, 06:05 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 776
Thanks: 0
Thanked 141 Times in 140 Posts
Norm is on a distinguished road

I'm feeling Fine
Default Re: Problem Using DataStream

Quote:
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.
Reply With Quote
The Following User Says Thank You to Norm For This Useful Post:
rrahulvverma (28-07-2010)
  #5 (permalink)  
Old 27-07-2010, 07:32 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,342
Thanks: 5
Thanked 284 Times in 256 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
rrahulvverma (28-07-2010)
  #6 (permalink)  
Old 29-07-2010, 08:59 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 776
Thanks: 0
Thanked 141 Times in 140 Posts
Norm is on a distinguished road

I'm feeling Fine
Default Reporting spam

How to report this junk?
Reply With Quote
  #7 (permalink)  
Old 29-07-2010, 09:28 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 677
Thanks: 8
Thanked 149 Times in 141 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 11:31 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.