Datainputstream不能从键盘输入基本类型数据什么意思??

干干干就完了 2016-12-15 10:20:07
import java.io.*;
public class Ex9_6_1_InputInt {
public static void main(String[] args)throws IOException{
DataInputStream din=new DataInputStream(System.in);
System.out.print("输入一个整数:");
int x=din.readInt();
System.out.println("输入的整数是:"+x);
}
}
书上写的输入的数据是123而输出不是123,这是因为输入数据不符合基本数据类型的格式,从键盘提供的数据是字符的字节码表示,如果输入123,则只代表1,2,3三个字符的字节数据,绝不是代表整数123的字节码。。。
这句话是什么意思??字节码又是什么东西,键盘上输入的不就是字符么??请大神分析一下,我实在是不懂
...全文
450 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lipengzhu 2016-12-30
  • 打赏
  • 举报
回复
首先声明下面是从一个帖子转过来的: 从当前数据输入流中读取一个有符号的 32 位整数。 此方法从基本输入流中读入四个字节。 如果读入的字节,顺序为 b1, b2,b3 和 b4, 满足 0 <= b1, b2,b3,b4 <= 255, 那么结果等于: (b1 << 24) | (b2 << 16) + (b3 << 8) +b4 该方法将一直阻塞,直到此四个字节数据被读入,或检测到了数据流尾或抛出异常。 返回值: 当前输入流的下四个字节,解释为一个 int。 抛出: EOFException 如果在读入四个字节前到达了文件尾。 抛出: IOException 如果发生某个 I/O 错误。
萧乡月夜 2016-12-16
  • 打赏
  • 举报
回复
这个方法是读取字节用的,返回int值

    /**
     * See the general contract of the <code>readInt</code>
     * method of <code>DataInput</code>.
     * <p>
     * Bytes
     * for this operation are read from the contained
     * input stream.
     *
     * @return     the next four bytes of this input stream, interpreted as an
     *             <code>int</code>.
     * @exception  EOFException  if this input stream reaches the end before
     *               reading four bytes.
     * @exception  IOException   the stream has been closed and the contained
     *             input stream does not support reading after close, or
     *             another I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    public final int readInt() throws IOException {
soton_dolphin 2016-12-16
  • 打赏
  • 举报
回复
你自己试过吗?

62,628

社区成员

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

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