北邮java oj 作业 问题 B: 二进制文件读取

文章描述了一个Java程序,该程序从二进制文件dict.dic中读取由特定代码写入的数据。输入是一个整数,指示要读取的循环次数。程序按顺序输出对应循环内的整数、布尔值、双精度浮点数和字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

已知一个二进制文件,文件名为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);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值