Updated on 2022-06-01 GMT+08:00

Reading a File

Function Description

Read data from a specified file in HDFS.

Close all requested resources after reading a file.

Sample Code

The following provides code snippets for reading a file. For complete codes, see the HdfsMain class in com.huawei.bigdata.hdfs.examples.

    /**
   * Read a file.
   *
   * @throws IOException
   */
  private void read() throws IOException {
    String strPath = DEST_PATH + File.separator + FILE_NAME;
    Path path = new Path(strPath);
    FSDataInputStream in = null;
    BufferedReader reader = null;
    StringBuffer strBuffer = new StringBuffer();

    try {
      in = fSystem.open(path);
      reader = new BufferedReader(new InputStreamReader(in));
      String sTempOneLine;

      // Write data to a file.
      while ((sTempOneLine = reader.readLine()) != null) {
        strBuffer.append(sTempOneLine);
      }

      System.out.println("result is : " + strBuffer.toString());
      System.out.println("success to read.");

    } finally {
      // Resources must be closed.
      close(reader);
      close(in);
    }
  }