DataInputStream是读2进制格式的文件(一般是通过DataOutputStream写入的),这样的文件你直接打开看都是乱码。
向你这个问题,应该先以字符串的方式读进来,再转换成整形就可以了。

解决方案 »

  1.   

    没有办法达到你的要求,
    自己写函数好了
    至于抛Exception 是因为你在main函数后面throws IOException了public static void main(String[] args){
       try
       {
    DataInputStream in = new DataInputStream(
    new StringBufferInputStream("45 65 123"));
    int a;
    while((a=in.readInt()) != -1) System.out.println(a);         
       }
       catch (Exception e)
       {
       }
    }
      

  2.   

    小鱼儿能不能告诉我readint的作用?
    你的意思是不是先把数字当作String读再转换成数字,这样的话不是效率很低?
      

  3.   

    旺旺写的和我写的有什么不同?
    多了try和catch还是不能解决我的问题啊!那些up up叫的先回答问题,有道理一定给分
      

  4.   

    这样和c是一样的,异常的原因是string->int出现了问题
    import java.io.*;
    import java.util.*;
    public class ioTest {
      public static void main(String[] args) throws IOException {
        DataInputStream in = new DataInputStream(
            new StringBufferInputStream("45 65 123"));
        String s=in.readLine();
        int a;
        StringTokenizer t=new StringTokenizer(s," ");
        while(t.hasMoreTokens()){
        a=Integer.parseInt(t.nextToken());
        System.out.println(a);
        }
      }
    }
      

  5.   

    I wrote a test program for you to demonstrate how System.in works.
    public class test
    {
       static public void main(String[] args)
       throws Exception
       {
           System.out.print("please input user name: ");
           
           int c = System.in.read();
           
           System.out.println();
           
           if(c == -1)
               System.out.println("Ctrl-C has been pressed!");
           
           System.out.print(c + " ");
           do
           {
               c = System.in.read();
               System.out.print(c + " ");
           }
           while(c != 10);
           
           System.out.println();
       }
    }
    1, System.in.read() would not return until you pressed RETURN or Ctrl-C which also termninates program as well;
    2, You can edit the line before press RETURN, program will never see those changes;
    3, Ctrl-C will cause System.in.read() returns -1 no matter how many other chars you have inputed;
    4, A RETURN carries two int values 13 and 10, so you can determinate an input stops here. Usually 13 and 10 are discarded, but you have to read them out from System.in.
      

  6.   

    谢谢上面各位大哥。
    我还有一点没明白:DataInputStream里有readInt(),readFloat()有什么用
    不能象c++那样直接读出来,而要到String 去转一下??
      

  7.   

    看看JAVA的API的解释,你就明白了
     readInt
    public int readInt()
                throws IOExceptionReads four input bytes and returns an int value. Let a be the first byte read, b be the second byte, c be the third byte, and d be the fourth byte. The value returned is:  
     (((a & 0xff) << 24) | ((b & 0xff) << 16) |
      ((c & 0xff) << 8) | (d & 0xff)) This method is suitable for reading bytes written by the writeInt method of interface DataOutput.再看这个程序:
      public static void main(String[] args) {
        try {
          DataInputStream in = new DataInputStream(
      new StringBufferInputStream("45 65 123"));
          byte a = in.readByte();
          byte b = in.readByte();
          byte c = in.readByte();
          byte d = in.readByte();
          System.out.println((((a & 0xff) << 24) | ((b & 0xff) << 16) |
      ((c & 0xff) << 8) | (d & 0xff)));    } catch (IOException ex) {
        }
    输出是875896886
    java里面,int类型是32位的,而字符是16位的,你在输入流里面放置的是字符,却想读出来int,得到的结果当然不是你想象的了
      

  8.   

    更直接地说吧:
    在c++中比如有这样一个字符串
    uilsdflk568sdf656
    我可以用下面地程序读出数字568和656
    #include<sstream>
    #include<string>
    #include<iostream>
    using namespace std;int main() {
        string s = "uilsdflk568sdf656";
        istringstream istr(s); 
        char c;
        while(istr>>c) {
            if(c>='0' && c<='9') {
                istr.putback(c);
                int n;
                istr>>n;
                cout<<n<<endl;
             }
        }
        return 0;
    }在java有没有类似的方法。我们都是java爱好者就在这里讨论讨论^_^
    我们地目的是在帮助别人地同时也提高自己吧,哈
      

  9.   

    是不是java这些功能要自己实现。它的输入输出流写的没c++的好?