readfully java,void readFully(byte[] b, int off, int len)

void readFully(byte[] b, int off, int len)

描述 (Description)

java.io.DataInputStream.readFully(byte[] b)方法从输入流中读取len个字节。

它会阻塞,直到出现下列情况之一 -b.length字节的输入数据可用。

检测到文件结尾。

如果发生任何I/O错误。

声明 (Declaration)

以下是java.io.DataInputStream.readFully(byte[] b, int off, int len)方法的声明 -public final void readFully(byte[] b, int off, int len)

参数 (Parameters)b - 目标缓冲区。

off - 数据的偏移量。

len - 要读取的字节数。

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)IOException - 如果发生任何I/O错误或流已关闭。

EOFException - 如果此输入流到达之前的末尾。

例子 (Example)

以下示例显示了java.io.DataInputStream.readFully(byte [] b,int off,int len)方法的用法。package com.iowiki;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

public class DataInputStreamDemo {

public static void main(String[] args) throws IOException {

InputStream is = null;

DataInputStream dis = null;

try {

// create file input stream

is = new FileInputStream("c:\\test.txt");

// create new data input stream

dis = new DataInputStream(is);

// available stream to be read

int length = dis.available();

// create buffer

byte[] buf = new byte[length];

// read the full data into the buffer

dis.readFully(buf, 4, 5);

// for each byte in the buffer

for (byte b:buf) {

char c = '0';

if(b!=0)

c = (char)b;

// prints character

System.out.print(c);

}

} catch(Exception e) {

// if any error occurs

e.printStackTrace();

} finally {

// releases all system resources from the streams

if(is!=null)

is.close();

if(dis!=null)

dis.close();

}

}

}

假设我们有一个文本文件c:/test.txt ,它具有以下内容。 这个文件将用作我们示例程序的输入 -Hello World!

让我们编译并运行上面的程序,这将产生以下结果 -0000Hello000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值