执行System.in.read()方法将从键盘缓冲区读入一个字节的数据,然而返回的16位的二进制数据,其低8位为键盘的ASCII码,高8位为0DataInputStream 和DataOutputStream类:它们分别为FilterInputStream和FilterOutputStream类的子类。同时DataInputStream 和DataOutputStream类由于分别实现了DataInput和DataOutput接口中定义的独立于具体机器的带格式的读写操作,从而可以实现对Java中的不同类型的基本类型数据的读写。
copy的幻灯片。不知道有没有用处,我也刚学

解决方案 »

  1.   

    可以接受其他类型的。
    因为在类System中in的定义是
    public static final InputStream in也就是这个in这个变量是一个public的inputStream的变量。
    那么很简单的比如
    System.in=new BufferedInputStream();
    就将起换为一个BufferedInputStream
      

  2.   

    可以嗎?
    System.in 已經final了呀?
      

  3.   

    执行System.in.read()方法将从键盘缓冲区读入一个字节的数据,然而返回的16位的二进制数据,其低8位为键盘的ASCII码,高8位为0
    既然都可以得到二进制码了,在转换别的不是很简单吗?
      

  4.   

    BufferedInputStream in = new BufferedInputStream(System.in);
    in.read(byte[] b);
      

  5.   

    System.in.read()只能接受一个字符吗?
    如果接受一个输入的2位数字怎么办
      

  6.   

    回qqbz(qqbz)和 Nightlee(李晔) 
    抱歉没注意,谢谢提醒。
    楼主可以用System类的函数来设置in
    public static void setIn(InputStream in)
    Reassigns the "standard" input stream. 
    First, if there is a security manager, its checkPermission method is called with a RuntimePermission("setIO") permission to see if it's ok to reassign the "standard" input stream.那么用System.setIn(new BufferedInputStream())
    就可以了,以前的回复收回
      

  7.   

    liujiboy的两次回复都纯属胡说八道,
    拜托自己不懂就去实践实践,还跑来这里误人子弟
    BufferedInputStream()什么时候有的无参数构造函数??
    楼主的问题我已经回答得很清楚了
    BufferedInputStream in = new BufferedInputStream(System.in);
    in.read(byte[] b);
    以System.in作为BufferedInputStream的构造函数的参数
    生成的流具有缓冲特性
    可以一次读取多个字节到一个字节数组
      

  8.   

    setIn是thinking in java的写法
    至于BufferedInputStream的构造函数的参数,我相信楼主不会误解的。
    我这里只是起说明作用。
      

  9.   

    不要搬出thinking in java来唬人
    <thinking in java>第二版 P433页
    BufferedReader in = new BufferedReader(
                         new InputStreamReader(
                           System .in ))
    我写的代码没有将inputStream转换成Reader。是针对字节的敢问你的代码中setIn的作用是什么?
    BufferedInputStream的参数又是什么?
      

  10.   

    import java.io.*;
    class ShowFile{
    public static void main (String[] args) throws IOException{
    int i;
    FileInputStream fin;
    do{
    i=fin.read();
    if(i!=-1) System.out.println((char)i);
    }while(i!=-1);
    try{
    fin = new FileInputStream(args[0]);
    }
    catch(FileNotFoundException e){
    System.out.println("not exist");
    }
    catch(ArrayIndexOutOfBoundsException e){System.out.println("last exit");
    }



    fin.close();
    }
    }
      

  11.   

    自己在里面找setin的作用吧,这是sun的System类的源代码。其中in的初始化是在initializeSystemClass函数的
    setIn0(new BufferedInputStream(fdIn));//setIn实际调用的是setIn0准确的说System.in本身就是一个BufferedInputStream,你的回复不是也没有意义吗?
    楼主想接受自定义的那么用自己写一个Stream替换setIn不是更好吗?(我是这么理解的就这么回)
    我写的BufferedInputStream就是举个例子吧了,我写XXXXXInputStream并且空常数,这总是行了吧?
    /*
     * @(#)System.java 1.131 03/01/29
     *
     * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */package java.lang;import java.io.*;
    import java.util.Properties;
    import java.util.PropertyPermission;
    import java.util.StringTokenizer;
    import java.security.AccessController;
    import java.security.PrivilegedAction;
    import java.security.AllPermission;
    import sun.net.InetAddressCachePolicy;
    import sun.reflect.Reflection;
    import sun.security.util.SecurityConstants;
    public final class System {
       .....
       .....
       .....
       
        public final static InputStream in = nullInputStream();
       .....
       .....
       .....        public static void setIn(InputStream in) {
    checkIO();
    setIn0(in);
        }
       .....
       .....
       .....
        private static native void setIn0(InputStream in);
       .....
       .....
       .....
         private static InputStream nullInputStream() throws NullPointerException {
    if (currentTimeMillis() > 0)
        return null;
    throw new NullPointerException();
        }
           private static void initializeSystemClass() {
    props = new Properties();
    initProperties(props);
    sun.misc.Version.init();
    FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
    FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
    FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
    setIn0(new BufferedInputStream(fdIn));
    setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); // Load the zip library now in order to keep java.util.zip.ZipFile
    // from trying to use itself to load this library later.
    loadLibrary("zip");        // Currently File.deleteOnExit is built on JVM_Exit, which is a
            // separate mechanism from shutdown hooks. Unfortunately in order to
            // work properly JVM_Exit implicitly requires that Java signal
            // handlers be set up for HUP, TERM, and INT (where available). If
            // File.deleteOnExit were implemented in terms of shutdown hooks this
            // call to Terminator.setup() could be removed.
            Terminator.setup(); // Set the maximum amount of direct memory.  This value is controlled
    // by the vm option -XX:MaxDirectMemorySize=<size>.  This method acts
    // as an initializer only if it is called before sun.misc.VM.booted().
      sun.misc.VM.maxDirectMemory(); // Subsystems that are invoked during initialization can invoke
    // sun.misc.VM.isBooted() in order to avoid doing things that should
    // wait until the application class loader has been set up.
    sun.misc.VM.booted();
        }
    }
    下面是thinking in java第3版中的一个用法,我的是电子版,第几页我不知道
    public class Redirecting {
      // Throw exceptions to console:
      public static void main(String[] args)
      throws IOException {
        PrintStream console = System.out;
        BufferedInputStream in = new BufferedInputStream(
          new FileInputStream("Redirecting.java"));
        PrintStream out = new PrintStream(
          new BufferedOutputStream(
            new FileOutputStream("test.out")));
        System.setIn(in);
        System.setOut(out);
        System.setErr(out);
        BufferedReader br = new BufferedReader(
          new InputStreamReader(System.in));
        String s;
        while((s = br.readLine()) != null)
          System.out.println(s);
        out.close(); // Remember this!
        System.setOut(console);
      }
    }
    我的解决方法是替换这个底层类,而不是在底层类上包裹一层。
    这个底层的类重写InputStream的read函数,ASCII在这一层转换为数字。
    楼主觉得哪个合适可以自己决定。
      

  12.   

    按照我的思路实现的两个类
    XXXXInputStream .java
    import java.io.*;
    public class XXXXInputStream extends DataInputStream 
    {
    private static FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
    public XXXXInputStream(){

    super(fdIn); }
    public XXXXInputStream(InputStream in)
    {
    super(in);
    }
    public synchronized int read() throws IOException
    {
    int num=-1;
    try{
    num=new Integer(super.readLine()).intValue();
    }
    catch(NumberFormatException e)
    {
    return -1;
    }
             //被5和3整除,否则返回-1
    if(num%5==0 && num%3==0)
    return num;
    else
    return -1;
    }
    }
    Test.jav是测试程序
    import java.io.*;
    public class Test 
    {
    public static void main(String[] args) throws Exception
    {
    InputStream oldin=System.in;
    System.setIn(new XXXXInputStream());
    if(System.in.read()==-1)
    System.out.println("input error");
    else
    System.out.println("input right");
    System.setIn(oldin);
    }
    }
    该代码已经测试仅作为说明最基本的方法。具体代码需要楼主自己写。
      

  13.   

    楼上的兄弟,思路很让我佩服哦,纯正的oop
    敬佩ing。。
      

  14.   

    TO liujiboy
    我没有看过thinking in java第三版
    对setIn的作用不熟悉,在此我收回前面说的话
    看完了你的代码,我有两点意见:
    1.System.in并不是BufferedInputStream
    public static final InputStream in
    The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.
    他是个标准InputSteam2.没必要简单问题复杂化
    IO系统已经提供了丰富的功能给我们
    其中有BufferedInputStream和DataInputStream
    分别提供了缓冲输入和数据输入的功能
    这两者结合System.in
    就能够实现缓冲"读取数据类型的标准输入流"
    充分利用现有资源提高开发效率才是最重要的
      

  15.   

    System.in是一个InputStream,但是在sun的System类的实现中System.in被初始化为一个BufferedInputStream,你可以查Sun的代码(上面也给出来了)。当然我的解决方法确实是复杂的方法。但是要看楼主对流控制的力度,简单的控制当然不用这么复杂,而复杂的控制,我的方法更为简单。同时我的方法对客户端的代码影响是很小的。如果把setIn也封装起来,那么客户程序完全可以不知道这个流类的存在而像通常一样使用System.in。复杂是相对的。楼主完全可以按照自己的需要选择。再次提醒的是,我写的代码只是为了说明,直接的从DataInputStream继承下来,不是个好方法。居体的类肯定是在代码上复杂的。OOP的很多方法是用代码的冗余,来解决修改带来的痛苦。