package hjy.hadoop;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class MyFSDataInputStream {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
FileSystem fs = null;
FSDataInputStream fsdInputStream = null;
FileOutputStream fos = null;
fos = new FileOutputStream("d:/hhd.txt");
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hjy:9000");
fs = FileSystem.get(new URI("hdfs://hjy:9000"),conf,"hadoop");
Path srcPath = new Path("hdfs://hjy:9000/aa/qingshu.txt");
fsdInputStream = fs.open(srcPath);
byte[] buff = new byte[1024];
int readCount = 0;
readCount = fsdInputStream.read(buff);
if(readCount==-1) {
System.out.println("文本为空!");
System.exit(0);
}
while (readCount != -1) {
fos.write(buff, 0, readCount);
System.out.println(new String(buff, 0, readCount)); //阅读文本内容
readCount = fsdInputStream.read(buff); //下载文本内容
}
fos.close();
}
}
截图: