Java Reference
In-Depth Information
program, I decided to just use a DataInputStream and FileInputStream. Study
the ChainDemo and ReadData programs and try to determine the output of
ReadData, which is shown in Figure 16.4.
import java.io.*;
public class ReadData
{
public static void main(String [] args)
{
try
{
FileInputStream fileIn =
new FileInputStream(“data.txt”);
DataInputStream dataIn = new DataInputStream(fileIn);
System.out.println(dataIn.readUTF());
int counter = dataIn.readInt();
double sum = 0.0;
for(int i = 0; i < counter; i++)
{
double current = dataIn.readDouble();
System.out.println(“Just read “ + current);
sum += current;
}
System.out.println(“\nAverage = “ + sum/counter);
dataIn.close();
fileIn.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Figure 16.4
Output of the ReadData program.
Search WWH ::




Custom Search