Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Marek;
- import java.io.*;
- /**
- * Sum all the floating points in a binary file
- * Suppose a binary data file named Exercise17_03.dat has been created and its data are created using
- * writeDouble(double) in DataOutputStream. The file contains an unspecified number of floating points
- * Write a program to find the sum of the floating points
- */
- /**
- * So what to do ?
- * I create a file, Exercise17_03.dat ( not sure if it is correct ), and pars sme floating points.
- * i must input the files content and sum the floating point.
- */
- public class Main {
- public static void main(String[] args) {
- String fileName = "Exercise17_03.dat";
- try {
- try (
- DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(fileName))
- ) {
- //Created some input for testing.
- dataOutputStream.writeFloat((float) Math.random() * 10);
- dataOutputStream.writeFloat((float) Math.random() * 10);
- dataOutputStream.writeFloat((float) Math.random() * 10);
- }
- try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(fileName))) {
- float theFloatSum = 0;
- while (dataInputStream.available() > 0) {
- float nextFloat = dataInputStream.readFloat();
- System.out.println(nextFloat);
- theFloatSum += nextFloat;
- }
- System.out.println(theFloatSum);
- } catch (EOFException e) {
- System.out.println("End of file was read. ");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment