读写基本数据类型和String
2008-01-05 09:01:35 来源:WEB开发网核心提示:import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.FileInputStream;import java.io.FileOutputStream;import
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(
"j2medev.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('\t');
out.writeInt(units[i]);
out.writeChar('\t');
out.writeChars(descs[i]);
out.writeChar('\n');
}
out.close();
// read it in again
DataInputStream in = new DataInputStream(new FileInputStream(
"j2medev.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();
}
}
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(
"j2medev.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('\t');
out.writeInt(units[i]);
out.writeChar('\t');
out.writeChars(descs[i]);
out.writeChar('\n');
}
out.close();
// read it in again
DataInputStream in = new DataInputStream(new FileInputStream(
"j2medev.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();
}
}
[]
- ››数据库大型应用解决方案总结
- ››数据结构2--数组
- ››数据源架构模式的活动记录
- ››数据结构C#版线性表(Data Structure)之单链表(Lin...
- ››数据结构C#版线性表(Data Structure)之顺序表(顺序...
- ››数据就是利润:在欧洲两端,IBM Information Cham...
- ››数据架构师: 您要治理什么?
- ››数据驱动的网络营销和网站运营笔记
- ››数据点:Windows Azure 表存储:不同于传统数据库...
- ››数据医生:Alamance Regional Medical Center 通过...
- ››数据显示iPhone 4销售业绩不及Android手机
- ››数据仓库中复杂报表SQL语句写法
更多精彩
赞助商链接