已知一个二进制文件,文件名为dict.dic。它是由下边的代码生成。 int a ; boolean b = true ; double c , f ; String d ; DataOutputStream out = new DataOutputStream( new FileOutputStream("dict.dic")); Scanner cin = new Scanner(System.in); for (int i = 0 ; i < 5 ; i ++ ) { a = cin.nextInt() ; f = Math.random() ; if ( f > 0.5 ) b = true ; else b = false ; c = cin.nextDouble() ; d = cin.next() ; out.writeInt(a); out.writeBoolean(b); out.writeDouble(c); out.writeUTF(d); } out.close(); // Remember this! cin.close(); } 你的任务是写一段程序,将这些内容读出来。
时间: 2024-01-14 18:02:45 浏览: 19
可以使用DataInputStream来读取这个二进制文件。以下是读取的代码:
```
DataInputStream in = new DataInputStream(new FileInputStream("dict.dic"));
for (int i = 0; i < 5; i++) {
int a = in.readInt();
boolean b = in.readBoolean();
double c = in.readDouble();
String d = in.readUTF();
System.out.println("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d);
}
in.close();
```
这段代码会依次读取int、boolean、double和String类型的数据,并将它们输出到控制台。需要注意的是,读取的顺序必须和写入的顺序相同。同时,在读取完所有数据后,需要关闭DataInputStream。
相关问题
题目描述 已知一个二进制文件,文件名为dict.dic。它是由下边的代码生成。 int a ; boolean b = true ; double c , f ; String d ; DataOutputStream out = new DataOutputStream( new FileOutputStream("dict.dic")); Scanner cin = new Scanner(System.in); for (int i = 0 ; i < 5 ; i ++ ) { a = cin.nextInt() ; f = Math.random() ; if ( f > 0.5 ) b = true ; else b = false ; c = cin.nextDouble() ; d = cin.next() ; out.writeInt(a); out.writeBoolean(b); out.writeDouble(c); out.writeUTF(d); } out.close(); // Remember this! cin.close(); } 你的任务是写一段程序,将这些内容读出来。 输入 为一个整数,只可能是1,2,3,4,5之一。 输出 将dict.dic中的相关内容输出到屏幕上,如果输入是1,则输出那段代码第一次循环写入的内容,如果输入是2,则输出那段代码第二次循环写入的内容,依此类推。 形式如样例。double类型的不是保留一位小数,直接输出就可以了。
以下是实现题目要求的Java程序:
```java
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
int a;
boolean b;
double c, f;
String d;
DataInputStream in = new DataInputStream(new FileInputStream("dict.dic"));
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 0; i < n; i++) {
a = in.readInt();
b = in.readBoolean();
c = in.readDouble();
d = in.readUTF();
System.out.println(a + " " + b + " " + c + " " + d);
}
in.close();
cin.close();
}
}
```
程序首先读入一个整数n,表示需要输出dict.dic中的第几个记录。然后使用DataInputStream从dict.dic中读取记录,依次输出到屏幕上。
需要注意的是,在读取double类型数据时,需要使用readDouble()方法,而不是nextDouble()方法。同时,输出double类型数据时,不需要保留一位小数,直接输出即可。
希望这个程序对你有所帮助!
已知一个二进制文件,文件名为dict.dic。它是由下边的代码生成。 int a ; boolean b = true ; double c , f ; String d ; DataOutputStream out = new DataOutputStream( new FileOutputStream("dict.dic")); Scanner cin = new Scanner(System.in); for (int i = 0 ; i < 5 ; i ++ ) { a = cin.nextInt() ; f = Math.random() ; if ( f > 0.5 ) b = true ; else b = false ; c = cin.nextDouble() ; d = cin.next() ; out.writeInt(a); out.writeBoolean(b); out.writeDouble(c); out.writeUTF(d); } out.close(); // Remember this! cin.close(); } 你的任务是写一段程序,将这些内容读出来。
下面是一个读取 dict.dic 文件的代码范例:
```java
import java.io.*;
import java.util.Scanner;
public class ReadDict {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new FileInputStream("dict.dic"));
for (int i = 0; i < 5; i++) {
int a = in.readInt();
boolean b = in.readBoolean();
double c = in.readDouble();
String d = in.readUTF();
System.out.println("a = " + a + ", b = " + b + ", c = " + c + ", d = " + d);
}
in.close();
}
}
```
这个程序使用了 `DataInputStream` 来读取二进制文件,其中的 `readInt()`、`readBoolean()`、`readDouble()` 和 `readUTF()` 方法与 `DataOutputStream` 中的对应方法配合使用。最后别忘了关闭输入流。