Path file = new Path("/user/hadoop/input/file.txt"); FileSystem fs = file.getFileSystem(conf); FSDataInputStream inputStream = fs.open(file); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } reader.close(); fs.close();
Path file = new Path("/user/hadoop/output/file.txt"); FileSystem fs = file.getFileSystem(conf); FSDataOutputStream outputStream = fs.create(file); outputStream.writeBytes("Hello World"); outputStream.close(); fs.close();In this example, we are creating a file "file.txt" in the output directory in HDFS and writing the string "Hello World" to it. The package library used in these examples is org.apache.hadoop.fs.