题目描述
已知一个二进制文件,文件名为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类型的不是保留一位小数,直接输出就可以了。
样例输入 Copy
1
样例输出 Copy
100 false 72.5 helloworld
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class biotextread {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int input = sc.nextInt();
int i=0;
int a=0;
boolean b=true;
double c=0.0;
String d=" ";
try ( DataInputStream in = new DataInputStream(new FileInputStream("dict.dic"));) {
while(i!=input) {
a = in.readInt();
b = in.readBoolean();
c = in.readDouble();
d = in.readUTF();
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}