j2me 点滴2

本文详细解释了DataInputStream中的三种read方法:read(), read(byte[] b), 与read(byte[] b, int off, int len)的功能及用法,并通过实例说明了如何使用这些方法来从输入流中读取数据。

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

 DataInputStream 中的 三个read方法比较
int read()
          Reads the next byte of data from this input stream.
 int read(byte[] b)
          See the general contract of the read method of DataInput.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this input stream into an array of bytes

注:read(),读取DataInputStream中的字节,每次读取一个字符,例如:<tip>hello</tip>,每read()一次,读取字符串中的一个字符,返回该字符的ASCII值(0---255)。
例如:     dis = new DataInputStream( hc.openInputStream() );           
            int ch;           
            while( ( ch = dis.read() ) != -1 ) {               
                responseMessage.append( (char)ch );//responseMessage 是string。              
            }
 
 ch是read()返回值,通过(char)ch 可以将ch还原成字符。

int read(byte[] b)
          See the general contract of the read method of DataInput.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this input stream into an array of bytes
这两个方法的返回值则是将inputstream中的内容读取到byte[]中,返回的是读取的长度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值