import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreamDemo {
public static void main(String[] args) {
// TODO 自動生成的方法存根
//實現基本類型的讀寫
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) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}finally{
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
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) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
catch (EOFException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}finally{
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}
System.out.println(total);
}
}