DataOutputStream和DataInputStream使用的案例
2007-07-16 23:48
483 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.DataInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.DataOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.EOFException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class DataIODemo1 ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) throws IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataOutputStream out = new DataOutputStream(new FileOutputStream(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Java2s.txt"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
double[] prices = ...{ 19.99, 9.99, 15.99, 3.99, 4.99 };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int[] units = ...{ 12, 8, 13, 29, 50 };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] descs = ...{ "Java", "Source ", "and",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
"Support."};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < prices.length; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeDouble(prices[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeChar(' ');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeInt(units[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeChar(' ');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeChars(descs[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.writeChar(' ');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// read it in again
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataInputStream in = new DataInputStream(new FileInputStream(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Java2s.txt"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double price;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int unit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String desc;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double total = 0.0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while (true) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
price = in.readDouble();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.readChar(); // throws out the tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unit = in.readInt();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.readChar(); // throws out the tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
desc = in.readLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println( unit );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println( desc );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println( desc );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
total = total + unit * price;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (EOFException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- cobertura代码测试覆盖率使用案例
- Servlet使用适配器模式进行增删改查案例(DeptDaoImpl.java)
- Servlet使用适配器模式进行增删改查案例(BaseServiceImplTest.java)
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
- Netty in Action (十一) 第四章 第四部分 传输服务使用案例
- 使用NIO读取文件 Java实现案例
- mule使用案例企业服务总线ESB案例activeMQ转换成xml
- Linux远程备份工具Rsync使用案例
- 【成功案例】移动App开发商使用Node.js SmartMachine更换集群
- Ajax详解及其案例分析之如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表
- repeater实现样式设置,并添加删除按钮,参考课上老师做的案例。数据表添加字段status,使用repeater实现禁用与启用。repeater添加编辑按钮,和Edit.aspx页面。
- 个人作业2——英语学习APP案例分析(必应词典的使用)
- SharedPereferences使用案例介绍
- java学习记录--ThreadLocal使用案例
- SimpleAdapter使用案例
- redis在PHP中的基本使用案例
- 腾讯游戏使用Microsoft Service Fabric案例
- C#中使用XML——基于DOM的案例分析
- 使用设计模式优化设计案例<一>
- vue 2 使用Bus.js进行兄弟(非父子)组件通信 简单案例