0

I created my sales.dat with these code:

public static void main(String[] args) throws IOException {

    DataOutputStream output = new DataOutputStream
            (new FileOutputStream("E:/HOMEWORK/OOP/OOP-java/src/week2/sales.dat"));

    output.writeChars("San Francisco: ");
    output.writeDouble(1123.456);


}

And when I opened sales.dat, here it is:

enter image description here

I wrote another class to read .dat file:

public static void main(String[] args) throws IOException {

    File file = new File("E:/HOMEWORK/OOP/OOP-java/src/week2/sales.dat");
    FileReader reader = new FileReader(file);

    char[] buffer = new char[4*1024];
    int read = -1;

    StringBuilder builder = new StringBuilder();
    while( (read = reader.read(buffer)) !=-1){
        builder.append(buffer,0,read);
    }
    System.out.println(builder);


//              

}

And the result is :

enter image description here

So plz help me, the output must be :" San Francisco: 1123.456 "

user207421
  • 305,947
  • 44
  • 307
  • 483
MInh Đinh
  • 11
  • 2

4 Answers4

0

You should use DataInputStream to read.

public static void main(String[] args) throws IOException {
        String name = "...";
        DataOutputStream output = new DataOutputStream
                (new FileOutputStream(name));
        output.writeUTF("San Francisco: ");
        output.writeDouble(1123.456);
        output.close();
}

public static void main(String[] args) throws IOException {
    String name = "C:/Users/hsivakumar/Downloads/sales.dat";

    DataInputStream reader = new DataInputStream(new FileInputStream(name));

    StringBuilder builder = new StringBuilder();
    builder.append(reader.readUTF());
    builder.append(reader.readDouble());
    reader.close();
    System.out.println(builder);
}
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
0

Your expectations are incorrect. writeDouble() writes binary, not readable text. Your reading program should use readDouble(), if you can figure out how many chars to read before it. Your output file is poorly designed.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

You need to use a DataInputStream to display the double value :

  public static void main(String[] args)
        throws IOException {

        DataOutputStream output = new DataOutputStream(new FileOutputStream("c:/tmp/sales.dat"));
        output.writeUTF("San Francisco: ");
        output.writeDouble(1123.456);
        output.close();

        DataInputStream reader = new DataInputStream(new FileInputStream("c:/tmp/sales.dat"));
        String test = String.format("%s%s", reader.readUTF(), reader.readDouble());
        System.out.println(test);

    }
Gab
  • 11
  • 1
  • 5
-1

Okay it seems the problem you are experiencing, maybe because you didn't flush the bytes. Can you please check

output.flush();

After writing double to the file, and one more thing please try it by putting double value in a Variable first, and then write that variable.

let me know if that fix this problem for you. Cheers

Maher Shahmeer
  • 148
  • 1
  • 10