title: How can I merge files in directory in hdfs without using get merge command?
body: <p>I have gone through a programme in <strong>Hadoop In action</strong> for merging files on the go while copying from Local FS to HDFS.But while executing the code, I m getting array out of bound exception while running in eclipse. But when i created external jar file and run it Hadoop CLI, Empty file got created. If the code's logic is incorrect, is there any other way to merge multiple files in directory in hdfs without using get merge command?</p>
  
  <pre><code>import java.io.IOException;
  
  import org.apache.hadoop.conf.Configuration;
  
  import org.apache.hadoop.fs.FSDataInputStream;
  
  import org.apache.hadoop.fs.FSDataOutputStream;
  
  import org.apache.hadoop.fs.FileStatus;
  
  import org.apache.hadoop.fs.FileSystem;
  
  import org.apache.hadoop.fs.Path;
  
  public class PutMerge {
  
  public static void main(String[] args) throws IOException
  {
  Configuration conf = new Configuration();
  
  FileSystem hdfs = FileSystem.get(conf);
  FileSystem local = FileSystem.getLocal(conf);
  
  Path inputDir = new Path(args[0]);
  Path hdfsFile = new Path(args[1]);
  
  try {
  
  FileStatus[] inputFiles = local.listStatus(inputDir);
  FSDataOutputStream out = hdfs.create(hdfsFile);
  
  for(`int i=0; i&lt;inputFiles.length;i++)`
  
  System.out.println(inputFiles[i].getPath().getName());
  
  FSDataInputStream in = local.open(inputFiles[i].getPath());
  
  byte buffer[] = new byte[256];
  
  int bytesRead = 0;
  
  while( (bytesRead = in.read(buffer)) &gt; 0) {
  
  out.write(buffer, 0, bytesRead);
  
  }
  
  in.close();
  
  }
  
  out.close();
  
  } catch (IOException e) {
  
      e.printStackTrace();
  
  }
  
  }
  
  }
  </code></pre>