数据流对象DataInputStream、DataOutputStream的使用
数据流包括DataInputStream、DataOutputStream类,他们允许按照java的基础数据类型读写流中的数据。具体细节请查看java api;public class DataOutputTest {
public static void main(String[] args) throws IOException {
String username = "383065059";
String path = "F:/workplaceT/Test/src/" + username;
String inforTxt = "msg.db";
File infoFile = new File(path + "/" + inforTxt);
FileOutputStream fileOut = new FileOutputStream(infoFile);
DataOutputStream out = new DataOutputStream(fileOut);
//定义要保存的数据数组
double[] prices={19.90,12.56,18.90,14.99,20.00};//长度5
int[] amount={3,4,5,6,7,8};
String[] descs={"java ee","java se","oracle","sqlserver","android"};
//将prices,amount及descs中的数据以Tab键为分割保存到文件中。
for(int i=0;i<5;i++){
out.writeDouble(prices);
out.writeChar('\t');
out.writeInt(amount);
out.writeChar('\t');
out.writeUTF(descs);
out.writeChar('\t');
}
out.close();
//创建的数据输入流,将上面保存的文件再次打开并读取
FileInputStream fileIn = new FileInputStream(infoFile);
DataInputStream in=new DataInputStream(fileIn);
double price;
int amnt;
String desc;
double total=0.0;
for(int i=0;i<5;i++){
price=in.readDouble();
in.readChar();//扔掉tab
amnt=in.readInt();
in.readChar();
desc=in.readUTF();
in.readChar();
System.out.println("你订购了 "+amnt+"件 "+desc+",价格为 "+price);
total=total+amnt*price;
}
System.out.println("共计金额:"+total+"元");
in.close();
}
}
//读写为互逆操作,规律比较死。
页:
[1]