Results 1 to 6 of 6
Thread: Read binary files
- 03-03-2010, 08:05 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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
- 03-03-2010, 08:37 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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...
- 03-03-2010, 10:09 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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
- 03-03-2010, 10:13 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
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
- 03-04-2010, 12:49 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Can someone please help i am stuck
- 03-04-2010, 03:37 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Read/Write Files
By FlyNn in forum New To JavaReplies: 3Last Post: 02-06-2010, 08:45 PM -
Read and Open *.txt Files? help!!
By ashton in forum New To JavaReplies: 9Last Post: 11-01-2009, 03:42 PM -
[SOLVED] write&read a binary file
By tOpach in forum New To JavaReplies: 9Last Post: 05-01-2009, 12:28 AM -
How to use FTP to read files from server?
By user12345 in forum New To JavaReplies: 1Last Post: 03-18-2009, 12:46 PM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks