Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default weird character in DataOutputStream file

    hello...i'm just new to Java.

    I try to make a file (.dat or .txt) file to store Double data value, but after executing the file, I open the file and found weird character in there. It's supposed to be Double datatype value. Can somebody tell me why this happened?

    Thanks ^_^

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What was the value in the text file? What was the code to write to the file?(use code tags)

    [code]
    YOUR CODE HERE
    [/code]

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by digitalmalayan View Post
    I try to make a file (.dat or .txt) file to store Double data value, but after executing the file, I open the file and found weird character in there. It's supposed to be Double datatype value. Can somebody tell me why this happened?
    Um, I think I speak for most in saying that our mind-reading abilities are not as powerful as you might assume. You might want to show us the code, tell us the file type and even show the "weird" character if possible.
    Last edited by Fubarable; 06-25-2011 at 03:57 PM.

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

    Default

    If you write a double value to a file, it could be written in its binary format. If you want to be able to read the contents of the file you need to convert whatever you write to a String.

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

    Default

    edit: too slow again; what Norm said ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Well, I'm psychic and can see that the OP is writing double values in binary (by using the DataOutputStream) but expects them to be written in String form, hence the 'weird' characters ...
    Yeah, I'm not psychic but rather psycho, and I figured the same, but he really should show us the code.

  7. #7
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Hello again. This is my code ^_^

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.io.*;
    class cuba {
    	public static void main(String[] args)throws Exception{
    
    		double value[]= {1,2,3,4};
    
    		System.out.println(value);
    		double value_double = value[0]+value[1]+value[2]+value[3];
    
    		DataOutputStream out = null;
    
       		try {
          	out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
              	"try.dat")));
    
      		    {
           		 out.writeDouble(value_double);
         		}
        		} finally {
         		 out.close();
        		}
    		}
    	}

    this is not my original code but it's almost same like this. Can somebody tell me why I get weird character as output and file output. I'm very new to Java and i'm also not a clever student. Thanks for the help ^_^

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

    Default

    Have you read the API doc for the DataOutputStream class and the writeDouble method?
    Did you read any of the previous posts on this thread that describe what is happening when your code is executed?

    If not, please do.
    If you have any questions, about the API doc or the postings please ask.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What did it print to the file? You can look at the API and see how to write a string. you can make the double a string very easily with
    Java Code:
    double_value + ""
    Also, consider looking up and using print writer.

  10. #10
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Weird problem running a batch file
    By d3n1s in forum Advanced Java
    Replies: 7
    Last Post: 06-19-2011, 01:34 AM
  2. File Download .zip is downloading weird
    By hardcorebadger in forum New To Java
    Replies: 0
    Last Post: 02-25-2011, 08:08 PM
  3. Weird download file problem
    By bubbless in forum Advanced Java
    Replies: 12
    Last Post: 05-28-2010, 03:14 PM
  4. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  5. File I/O with DataOutputStream
    By Tzaphiel in forum New To Java
    Replies: 0
    Last Post: 12-16-2007, 09:39 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
  •