HDFS File Write – Java Program

In the previous we have seen how to read a file from HDFS. In this blog we will see how to write a file from local file system to HDFS.

Program Flow:

Create hadoop filesystem object

Create a Output stream on the hadoop filesystem for  given path

Get input stream for the local file using FileInputstream

Copy the bytes from input stream to output stream using copyBytes method.

Please note that code doesn’t contain any error handling

public class Main extends Configured implements Tool{

 public static void main(String[] args) throws Exception {
 int exitCode=ToolRunner.run(new Main(), args);
 System.out.println("Exit code "+exitCode);


 public int run(String[] args) throws Exception {
 Configuration conf=new Configuration();
 FileSystem fs=FileSystem.get(conf);

 OutputStream os=fs.create(new Path(args[1]));
 InputStream is=new FileInputStream(args[0]);

 IOUtils.copyBytes(is, os, 4096, true); 
 return 0;


You can run the generated file as shown below


Full project is available here .

Add a Comment

Your email address will not be published. Required fields are marked *