您的位置:首页 > 大数据 > 人工智能

DataOutputStream和DataInputStream使用的案例

2007-07-16 23:48 483 查看

import java.io.DataInputStream;


import java.io.DataOutputStream;





import java.io.EOFException;


import java.io.FileInputStream;


import java.io.FileOutputStream;


import java.io.IOException;






public class DataIODemo1 ...{




public static void main(String[] args) throws IOException ...{


DataOutputStream out = new DataOutputStream(new FileOutputStream(


"Java2s.txt"));






double[] prices = ...{ 19.99, 9.99, 15.99, 3.99, 4.99 };




int[] units = ...{ 12, 8, 13, 29, 50 };




String[] descs = ...{ "Java", "Source ", "and",


"Support."};






for (int i = 0; i < prices.length; i++) ...{


out.writeDouble(prices[i]);


out.writeChar(' ');


out.writeInt(units[i]);


out.writeChar(' ');


out.writeChars(descs[i]);


out.writeChar(' ');


}


out.close();




// read it in again


DataInputStream in = new DataInputStream(new FileInputStream(


"Java2s.txt"));




double price;


int unit;


String desc;


double total = 0.0;






try ...{




while (true) ...{


price = in.readDouble();


in.readChar(); // throws out the tab


unit = in.readInt();


in.readChar(); // throws out the tab


desc = in.readLine();


System.out.println( unit );


System.out.println( desc );


System.out.println( desc );


total = total + unit * price;


}




} catch (EOFException e) ...{


}


in.close();


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: