|
||
|
|||
|
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();
}
}
|
| The Following User Says Thank You to copeg For This Useful Post: | ||
rrahulvverma (27-07-2010) | ||
|
|||
|
Quote:
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? |
|
||||
|
Quote:
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. |
| The Following User Says Thank You to Norm For This Useful Post: | ||
rrahulvverma (28-07-2010) | ||
|
||||
|
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]. |
| The Following User Says Thank You to helloworld922 For This Useful Post: | ||
rrahulvverma (28-07-2010) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
|
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 |