注册 登录
编程论坛 JAVA论坛

如何读取都是整数 的文件

秦时的明月夜 发布于 2015-06-19 11:13, 617 次点击
不用readline这种方法,可否一个一个读出来像这样的如何读取 datainputstream 里的readint 读出来的是整数但是 是时错误的数据 有明白的人讲解一下谢谢……
只有本站会员才能查看附件,请 登录
4 回复
#2
wp2319572015-06-19 11:21
没有回车和换行符???
#3
秦时的明月夜2015-06-19 11:30
没有回车和换行符,只有空格……
#4
calix2015-06-19 13:24
这个文件看起来都是整数,实际上是以字符形式存放的
如果文件内容不多,可以一次性读出来,用空格split
readInt方法适合于DataOutput中writeInt写进去的数据
API:This method is suitable for reading bytes written by the writeInt method of interface DataOutput.
程序代码:
public class IOTest {
    public static void main(String[] args) throws IOException{
        int length = 0;
        OutputStream out = new FileOutputStream("d:\\test.txt");
        DataOutput output = new DataOutputStream(out);
        for(; length < 20; length++){
            output.writeInt(length);
        }
        out.close();
      
        InputStream in = new FileInputStream("d:\\test.txt");
        DataInput input = new DataInputStream(in);
        while(length > 0){
            System.out.println(input.readInt());
            length --;
        }
        in.close();
    }
}

#5
秦时的明月夜2015-06-19 15:03
回复 4楼 calix
我明白了谢谢……
1