data io stream 的问题

phoenixv 2001-12-18 08:28:50
import java.io.*;

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

// write the data out
DataOutputStream out = new DataOutputStream(new
FileOutputStream("invoice1.txt"));

double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
int[] units = { 12, 8, 13, 29, 50 };
String[] descs = { "Java T-shirt",
"Java Mug",
"Duke Juggling Dolls",
"Java Pin",
"Java Key Chain" };

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("invoice1.txt"));

double price;
int unit;
StringBuffer 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
char chr;
desc = new StringBuffer(50);
char lineSep = System.getProperty("line.separator").charAt(0);
while ((chr = in.readChar()) != lineSep)
desc.append(chr);
System.out.println("You've ordered " +
unit + " units of " +
desc + " at $" + price);
total = total + unit * price;
}
} catch (EOFException e) {
}
System.out.println("For a TOTAL of: $" + total);
in.close();
}
}

上面折断程序的执行结果应该是
You've ordered 12 units of Java T-shirt at $19.99
You've ordered 8 units of Java Mug at $9.99
You've ordered 13 units of Duke Juggling Dolls at $15.99
You've ordered 29 units of Java Pin at $3.99
You've ordered 50 units of Java Key Chain at $4.99
For a TOTAL of: $892.8800000000001

可是在我的机器上执行的结果却是
You've ordered 12 units of Java T-shirt
???? Java Mug
???? at $19.99
For a TOTAL of: $239.88

我的系统是中文win98,不知道是不是和中文系统有关系,是不是在英文系统下能成功执行?各位有遇到过相同的情况的吗?怎么解决呢?谢谢。
...全文
20 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧