# Java WriteTo File

## Write Double To File

### source code

```
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteDoubleToFile {
public static void main(String[] args) {
String strFilePath = "C://FileIO//WriteDouble.txt";
try
{
//create FileOutputStream object
FileOutputStream fos = new FileOutputStream(strFilePath);
/*
* To create DataOutputStream object from FileOutputStream use,
* DataOutputStream(OutputStream os) constructor.
*
*/
DataOutputStream dos = new DataOutputStream(fos);
double d = 75000;
/*
* To write a double value to a file, use
* void writeDouble(double d) method of Java DataOutputStream class.
*
* This method writes specified double to output stream as 8 bytes value.
* Please note that the double value is first converted to long using
* Double.doubleToLongBits method and then long is written to
* underlying output stream.
*/
dos.writeDouble(d);
/*
* To close DataOutputStream use,
* void close() method.
*
*/
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
}
}
}
```

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

### hexdump

Double 类型使用64bits（8个bytes）来存储

float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the significand).

double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand

```
00000000: 40 F2 4F 80 00 00 00 00
```

### 转换的方法：

double和float使用IEEE754标准记录

- 整数部分为75000，转成二进制为10010010011111000，用指数表示1.0010010011111000 x 2^16
- double类型的偏移码是1023，所以1023+16=1039 转成二进制 10000001111
- 符号位为：0
- 尾数为：0010010011111000000000000000000000000000000000000000 最终

为：0100000011110010010011111000000000000000000000000000000000000000

16进制表示就是：40F24F8000000000

### 在线转换

https://gregstoll.com/~gregstoll/floattohex/ https://www.h-schmidt.net/FloatConverter/IEEE754.html

### IEEE754

IEEE754标准包含一组实数的二进制表示法。它有三部分组成：

- 符号位
- 指数位
- 尾数位

https://www.zhihu.com/question/21711083

## Convert-a-Number-from-Decimal-to-IEEE-754-Floating-Point-Representation

### 1. Choose single or double precision.

When writing a number in single or double precision, the steps to a successful conversion will be the same for both, the only change occurs when converting the exponent and mantissa. First we must understand what single precision means. In floating point representation, each number (0 or 1) is considered a “bit”. Therefore single precision has 32 bits total that are divided into 3 different subjects. These subjects consist of a sign (1 bit), an exponent (8 bits), and a mantissa or fraction (23 bits). Double precision, on the other hand, has the same setup and same 3 parts as single precision; the only difference is that it will be larger and more precise number. In this case, the sign will have 1 bit, the exponent will have 11 bits and the mantissa will have 52 bits. In this example will convert the number 85.125 into IEEE 754 single precision.

### 2. Separate the whole and the decimal part of the number.

Take the number that you would like to convert, and take apart the number so you have a whole number portion and a decimal number portion. This example will use the number 85.125. You can separate that into whole number 85, and the decimal 0.125.

### 3. Convert the whole number into binary.

This would be the 85 from 85.125, which will be 1010101 when converted into binary.

### 4. Convert the decimal portion into binary.

This would be the 0.125 from 85.125, which will be 0.001 when converted into binary.

### 5. Combine the two parts of the number that have been converted into binary.

For instance, the number 85 in binary is 1010101 and the decimal portion 0.125 in binary is .001. When you combine them using a decimal point, you end up with 1010101.001 as your final answer.

### 6. Convert the binary number into base 2 scientific notation.

You can convert the number into base 2 scientific notation by moving the decimal point over to the left until it is to the right of the first bit. These numbers are normalized which means the leading bit will always be 1. As for the exponent, the number of times that you moved the decimal will be your exponent in base 2 scientific notation.[4]
Remember that moving the decimal to the left will result in a positive exponent while moving the decimal to the right will result in a negative exponent.
For our example, you will need to move the decimal 6 times in order to get it to the right of the first bit. The resulting notation will be {\displaystyle 01.010101001*2^{6}}01.010101001*2^{6} , this number will be used in future steps.

### 7. Determine the sign of the number and display in binary format.

You will now determine if your original number is positive or negative. If the number is positive, you will record that bit as 0, and if it is negative, you will record that bit as 1.[5] Since your original number, 85.125, is positive, you will record that bit as 0. This will be the first bit out of the 32 total bits in your IEEE 754 single precision representation.

### 8.Get the exponent based on precision.

There are set biases for both single and double precision. The exponent bias for single precision is 127, which means we must add the base 2 exponent found previously to it. Thus, the exponent you will use is 127+6 which is 133. Double precision as perceived from the name is more precise and can hold larger numbers. Therefore its exponent bias is 1023. The same steps used for single precision apply here, so the exponent you can use to find double precision is 1029.

### 9. Turn the exponent into binary.

After you determine your final exponent, you will need to convert it into binary so that it could be used in the IEEE 754 conversion. For the example, you can convert the 133 that you found in the last step into 10000101.

### 10. Determine the mantissa.

The mantissa aspect, or the third part of the IEEE 754 conversion, is the rest of the number after the decimal of the base 2 scientific notation. You will just drop the 1 in the front and copy the decimal portion of the number that is being multiplied by 2. No binary conversion needed! For the example, the mantissa would be 010101001 from {\displaystyle 01.010101001*2^{6}}01.010101001*2^{6}.

### 11. Compile 3 parts into one final number.

Finally, you will compile all that we have calculated thus far into your conversion. It will first begin with a 0 or 1 bit that you determined in step 7 based on sign. For the example, you will have a 0 to start it. Next up, you will have the exponent section that you determined in step 9. For the example, your exponent will be 10000101. Now, you have the mantissa, which is the third and last part of the conversion. You derived this earlier when you took the decimal portion of the base 2 conversion. For the example, the mantissa would be 010101001. Finally, you combine these all together. The order should go sign-exponent-mantissa. After you connect those three binary numbers, you then fill out the rest of the mantissa with 0s. For the example the solution is 0 10000101 01010100100000000000000 as 85.125 converted into IEEE 754 format.