package java_basis;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.*;
public class DataStreamDemo {
public static void main(String [] args){
DataOutputStream dos = null ;
try{
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("IntegerData")));
for (int i = 0 ; i < 10 ; i++){
dos.writeDouble(Math.random()*1000);
}
dos.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(dos != null){
try{
dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
DataInputStream dis = null ;
double total = 0.0 ;
try{
dis = new DataInputStream(new BufferedInputStream(new FileInputStream("IntegerData")));
while(true){
total += dis.readDouble();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(EOFException e){
}catch(IOException e){
e.printStackTrace();
}finally{
if(dis != null){
try{
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
System.out.println("总计:" + total);
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.*;
public class DataStreamDemo {
public static void main(String [] args){
DataOutputStream dos = null ;
try{
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("IntegerData")));
for (int i = 0 ; i < 10 ; i++){
dos.writeDouble(Math.random()*1000);
}
dos.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(dos != null){
try{
dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
DataInputStream dis = null ;
double total = 0.0 ;
try{
dis = new DataInputStream(new BufferedInputStream(new FileInputStream("IntegerData")));
while(true){
total += dis.readDouble();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(EOFException e){
}catch(IOException e){
e.printStackTrace();
}finally{
if(dis != null){
try{
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
System.out.println("总计:" + total);
}
}