不单独说明了,代码中的注释很详细了
- /**
- *
- * Description: 这个例子用于展示Hadoop的FSDataInputStream的流定位能力
- *
- * @author charles.wang
- * @created Mar 13, 2012 9:21:34 AM
- *
- */
- public class FileSystemCatSeekable {
- public static void main(String [] args) throws Exception{
- //获取命令行参数
- String uri = args[0];
- Configuration conf = new Configuration();
- conf.set("hadoop.job.ugi", "root,root123");
- //打开一个Hadoop FileSystem ,用FileSystem的静态方法获取之
- FileSystem fs = FileSystem.get(URI.create(uri) ,conf);
- //打开一个InputStream 对象
- FSDataInputStream in = null;
- try{
- //让其指向FileSystem中由命令行提供的uri对应的路径
- in =fs.open(new Path (uri));
- //第一次读取
- IOUtils.copyBytes(in, System.out, 20, false);
- //让读头重新定位到文件起始地方
- in.seek(0);
- //第二次读取
- IOUtils.copyBytes(in, System.out, 20, false);
- }catch (Exception ex){
- ex.printStackTrace();
- }finally{
- in.close();
- }
- }
- }
转载于:https://blog.51cto.com/supercharles888/840725