Help Center/ MapReduce Service/ Developer Guide (Normal_3.x)/ HDFS Development Guide (Security Mode)/ Developing an HDFS Application/ Writing Data into an HDFS File
Updated on 2025-10-23 GMT+08:00
Writing Data into an HDFS File
Function
The process of writing data into a file is
- Use the create method in the FileSystem instance to obtain the output stream of writing files.
- Uses this data stream to write content into a specified file in the HDFS.
Close all requested resources after writing files.
Example Codes
The following is code snippets. For complete codes, see HdfsExample class in com.huawei.bigdata.hdfs.examples.
/**
* Create a file and write data into the file.
*
* @throws java.io.IOException
* @throws com.huawei.bigdata.hdfs.examples.ParameterException
*/
private void write() throws IOException {
final String content = "hi, I am bigdata. It is successful if you can see me.";
FSDataOutputStream out = null;
try {
out = fSystem.create(new Path(DEST_PATH + File.separator + FILE_NAME));
out.write(content.getBytes());
out.hsync();
LOG.info("success to write.");
} finally {
// make sure the stream is closed finally.
IOUtils.closeStream(out);
}
} Parent topic: Developing an HDFS Application
What is your overall rating for this page?
0
1
2
3
4
5
6
7
8
9
10
Very dissatisfiedVery satisfied
Thank you very much for your feedback. We will continue working to improve the documentation.
The system is busy. Please try again later.