查看Java帮助手册或其它资料,用“java.net.URL”和“org.apache.hadoop.fs.FsURLStreamHandlerFactory”编程完成输出HDFS中指定文件的文本到终端中。
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
public class FsUrl {
static{;
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
/**
* @param args
*/
public static void cat(String remoteFilePath){
try(InputStream in=new URL("hdfs","localhost",9000,remoteFilePath).openStream()){
IOUtils.copyBytes(in, System.out, 4096, false);
IOUtils.closeStream(in);
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String remoteFilePath="/Test/text.txt";
try{
System.out.println("去读文件:"+remoteFilePath);
FsUrl.cat(remoteFilePath);
System.out.println("\n 读取完成");
}catch(Exception e){
e.printStackTrace();
}
}
}
标签:总结,java,io,URL,每日,System,remoteFilePath,12.5,import From: https://www.cnblogs.com/clh628/p/17878601.html