Commit 7949c9e
committed
feat: implement DataOutputStream and DataInputStream example for float arrays
WHAT:
- Added `DataOutPutStreamExampleForFloat` class.
- Demonstrates writing and reading an array of float values to/from a file.
HOW:
1. Declared a float array with sample values.
2. Opened `FileOutputStream` wrapped in `DataOutputStream`.
3. First wrote array length using `writeInt()`.
4. Iterated over the array and wrote each float using `writeFloat()`.
5. Closed output streams.
6. Reopened the same file using `FileInputStream` + `DataInputStream`.
7. Read array length back using `readInt()`.
8. Read each float sequentially using `readFloat()` and printed values.
9. Closed input streams.
KEY POINTS:
- `DataOutputStream` and `DataInputStream` are paired classes to write
and read Java primitives in a binary format.
- Order of writing/reading must exactly match, else deserialization breaks.
- This avoids manual string parsing and ensures efficient binary storage.
REAL-WORLD APPLICATIONS:
- Saving numerical datasets (e.g., sensor values, ML model parameters).
- Game development (storing player stats, positions, scores).
- Finance apps (writing and reading currency/exchange rate values).
- Lightweight persistence without full database overhead.
BENEFITS:
- Compact binary storage (efficient space usage).
- Faster read/write compared to text parsing.
- Cross-platform consistency (same binary format for Java apps).
NEXT IMPROVEMENTS:
- Add try-with-resources to simplify stream closing.
- Wrap logic into methods (`writeFloatArray`, `readFloatArray`) for reusability.
- Add exception handling/logging for robustness.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4b77d1b commit 7949c9e
File tree
2 files changed
+13
-15
lines changed- Section23JavaIOStreams
- Serialisation Storing Data in a File/src
- src/MyJAVA
2 files changed
+13
-15
lines changedLines changed: 13 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
7 | 5 | | |
8 | | - | |
9 | | - | |
| 6 | + | |
| 7 | + | |
10 | 8 | | |
11 | 9 | | |
12 | | - | |
13 | | - | |
| 10 | + | |
| 11 | + | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | | - | |
| 18 | + | |
21 | 19 | | |
| 20 | + | |
22 | 21 | | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
| 25 | + | |
| 26 | + | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
32 | | - | |
33 | 31 | | |
34 | | - | |
| 32 | + | |
Binary file not shown.
0 commit comments