PositionedReadable接口

FSDataInputStream也实现了Positionreadable接口,指从指定偏移量处读取数据。

 

 

hdfs中文件数据为:

 

首先对第一个方法进行测试:

package test01;

import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class test02 {
	public static void main(String[] args) throws Exception {
		Configuration conf=new Configuration();
		URI uri=new URI("hdfs://192.168.137.128:8020");
		FileSystem fs=FileSystem.get(uri, conf);
		FSDataInputStream in= fs.open(new Path("/test.txt"));
		System.out.println("文件的总大小为:"+in.available());
		//首先读取全部内容:
		IOUtils.copyBytes(in, System.out, in.available());
		//重新定位:
		in.seek(0);
		//创建一个27长度的byte数组
		byte[] buffer=new byte[27];
		//in.read(position, buffer, offset, length)
		in.read(0, buffer, 0, 27);
		System.out.println(new String(buffer));
		//从第1为开始,从数组的第1为开始,读取长度26,从buffer中的第一位开始存取
		byte[] buffer1=new byte[27];
		in.seek(0);
		in.read(2,buffer1,1,26);
		System.out.println(new String(buffer1));
		//重新定位,测试readFully(position, buffer)只读取buffer.length的长度的数据
		in.seek(0);
		byte[] buffer3=new byte[26];
		in.readFully(1, buffer3);
		System.out.println(new String(buffer3));
		in.close();
		fs.close();
	}
}

输出结果为:

 

 由上分析可知: 

          第一个方法是从某个位置(position)开始读取自定义的长度(legth),然后从数组的某个位置(offset)开始存取数据

          最后一个方法是从某个位置开始,读取buffer.length个长度

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值