1Configuration conf = new Configuration();
2conf.addResource(new Path("C:\\hadoop-3.1.0\\etc\\hadoop\\core-site.xml"));
3conf.addResource(new Path("C:\\hadoop-3.1.0\\etc\\hadoop\\hdfs-site.xml"));
4
5BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6String filePath = "hdfs://localhost:9000/directory/file.txt";
7
8Path path = new Path(filePath);
9FileSystem fs = path.getFileSystem(conf);
10FSDataInputStream inputStream = fs.open(path);
11System.out.println(inputStream.available());
12String line = null;
13while((line = inputStream.readLine()) != null) {
14 System.out.println(line);
15}
16fs.close();
1Configuration conf = new Configuration();
2Path path = new Path("hdfs file path");
3FileSystem fs = FileSystem.get(path.toUri(), conf);
4BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(path)));
5try {
6 String line;
7 line=br.readLine();
8 while (line != null){
9 System.out.println(line);
10
11 // be sure to read the next line otherwise you'll get an infinite loop
12 line = br.readLine();
13 }
14} finally {
15 // you should close out the BufferedReader
16 br.close();
17}