G
G
go2goj2019-12-13 13:59:23
Java
go2goj, 2019-12-13 13:59:23

What encoding do DataOutputStream and DataInputStream work with?

What encoding do DataOutputStream and DataInputStream work with? For example:

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Soltuion {

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

    // Как записывает true:
    // записали:
    DataOutputStream fileOutput = new DataOutputStream(new FileOutputStream("C:\\testjava.txt"));
    fileOutput.writeBoolean(true);

    // читаем:
    FileInputStream fileStream = new FileInputStream("C:\\testjava.txt");
    System.out.println(fileStream.read()); // 1

    // Как записывает double:
    // записали:
    fileOutput.writeDouble(3.2);

    // читаем:
    System.out.println(fileStream.read()); // 64
    System.out.println(fileStream.read()); // 9
    System.out.println(fileStream.read()); // 153
    System.out.println(fileStream.read()); // 153
    System.out.println(fileStream.read()); // 153
    System.out.println(fileStream.read()); // 153
    System.out.println(fileStream.read()); // 153
    System.out.println(fileStream.read()); // 154
    System.out.println(fileStream.read()); // -1
  }
}

Here's how to see (decode) this information in a notepad later:
5df36ed03bbdf211686120.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
go2goj, 2019-12-14
@go2goj

DataOutputStream and DataInputStream work with their own special encoding, which can be found in the sources. This allows you to store a boolean value in 1 byte, etc.
Obvious persons in notepad cannot read this, because notepad does not know the above encoding. Can be read through DataInputStream.

D
Denis Zagaevsky, 2019-12-13
@zagayevskiy

In such variant - in any coding. You write pure pristine bytes there, they are not converted in any way. Then you read these bytes one by one.
https://docs.oracle.com/javase/7/docs/api/java/io/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question