Results 1 to 6 of 6
  1. #1
    ronaldo121 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default Read binary files

    I am strugg

    Write a program that reads a binary file of integers between 0 and 100, and prints:

    the largest number in the file;
    the smallest number in the file;
    the average of all the numbers in the file.

    The program should obtain the file name form the user (the command line).


    Could someone please help with this?

    I have this code!

    import java.io.*;

    import java.util.*;



    /** Demonstrate reading primitive type values from a binary file. */

    public class BinInputFileApp

    {

    public static void main(String arg[]) {

    File file = null;

    int i_data = 0;

    double d_data = 0.0;



    // Get the file from the argument line.

    if (arg.length > 0) file = new File(arg[0]);

    if (file == null) {

    System.out.println("Default: numerical.dat");

    file = new File("numerical.dat");

    }



    try {

    // Wrap the FileInputStream with a DataInputStream

    FileInputStream file_input = new FileInputStream(file);

    DataInputStream data_in = new DataInputStream(file_input);



    while (true) {

    try {

    i_data = data_in.readInt();

    d_data = data_in.readDouble();

    }

    catch(EOFException eof) {

    System.out.println("End of File");

    break;

    }

    // Print out the integer, double data pairs.

    System.out.printf("%3d. Data = %8.3e %n", i_data, d_data);

    }

    data_in.close();

    } catch(IOException e) {

    System.out.println("IO Exception =: " + e);

    }

    } // main

    } // class BinInputApp

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    I guess you posted example from:

    Learning Java - Chapter 9 : Java

    and that's fine. You could use other example and first WRITE those integers
    to file, and after that you can use second example for reading.

    So first write Writer with 100 randomly created int values from 0 to 100
    (or even use byte or short primitive data types)
    and later in reader use
    i_data = data_in.readInt(); (or writeByte...)

    While working on reading you have to:
    read all numbers from file,
    put them to some collection or int[] array
    close file.

    Now you write 3 small methods using that collection or int array[] for:
    the largest number in the file;
    the smallest number in the file;
    the average of all the numbers in the file.
    That is very basic stuff.

    it's easy...

  3. #3
    ronaldo121 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    got this code and now i have the .dat file which has 100 values

    import java.io.*;
    import java.util.*;

    /** Write a primitive type data array to a binary file.**/
    public class BinOutputFileApp
    {
    public static void main (String arg[]) {
    Random ran = new Random ();

    // Create an integer array and a double array.
    int [] i_data = new int[99];
    double [] d_data = new double[99];
    // and fill them
    for (int i=0; i < i_data.length; i++) {
    i_data[i] = i;
    d_data[i] = ran.nextDouble () * 10.0;
    }

    File file = null;
    // Get the output file name from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    // or use a default file name
    if (file == null) {
    System.out.println ("Default: numerical.dat");
    file = new File ("numerical.dat");
    }

    // Now write the data array to the file.
    try {
    // Create an output stream to the file.
    FileOutputStream file_output = new FileOutputStream (file);
    // Wrap the FileOutputStream with a DataOutputStream
    DataOutputStream data_out = new DataOutputStream (file_output);

    // Write the data to the file in an integer/double pair
    for (int i=0; i < i_data.length; i++) {
    data_out.writeInt (i_data[i]);
    data_out.writeDouble (d_data[i]);
    }
    // Close file when finished with it..
    file_output.close ();
    }
    catch (IOException e) {
    System.out.println ("IO exception = " + e );
    }
    } // main

    } // class BinOutputFileApp
    import java.io.*;
    import java.util.*;

    /** Demonstrate reading primitive type values from a binary file. **/
    public class BinInputFileApp
    {
    public static void main (String arg[]) {
    File file = null;
    int i_data = 0;
    double d_data = 0.0;

    // Get the file from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    if (file == null) {
    System.out.println ("Default: numerical.dat");
    file = new File ("numerical.dat");
    }

    try {
    // Wrap the FileInputStream with a DataInputStream
    FileInputStream file_input = new FileInputStream (file);
    DataInputStream data_in = new DataInputStream (file_input );

    while (true) {
    try {
    i_data = data_in.readInt ();
    d_data = data_in.readDouble ();
    }
    catch (EOFException eof) {
    System.out.println ("End of File");
    break;
    }
    // Print out the integer, double data pairs.
    System.out.printf ("%3d. Data = %8.3e %n", i_data, d_data );
    }
    data_in.close ();
    } catch (IOException e) {
    System.out.println ( "IO Exception =: " + e );
    }
    } // main
    } // class BinInputApp

  4. #4
    ronaldo121 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Now i need to read the largest number, smallest number and average number in that file?


    Can someone please help as i am new to this java and not sure

    i know the method for average would be sum of intergeRs divded by amount of intergers i.e 100

  5. #5
    ronaldo121 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Can someone please help i am stuck

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ronaldo121 View Post
    Now i need to read the largest number, smallest number and average number in that file?


    Can someone please help as i am new to this java and not sure

    i know the method for average would be sum of intergeRs divded by amount of intergers i.e 100
    Can you please take a paper and write down the logic, how find the maximum number from set of numbers. Take 4 numbers to take a start. Then can you please show what you have written there in the paper.

Similar Threads

  1. Read/Write Files
    By FlyNn in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 09:45 PM
  2. Read and Open *.txt Files? help!!
    By ashton in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:42 PM
  3. [SOLVED] write&amp;read a binary file
    By tOpach in forum New To Java
    Replies: 9
    Last Post: 05-01-2009, 01:28 AM
  4. How to use FTP to read files from server?
    By user12345 in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 01:46 PM
  5. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •