title: How do I send file name with file using sockets in Java?
body: <p>I have the following code which transfers file via Sockets. How do I send the file name?</p>
  
  <pre><code>Socket socket = new Socket("localhost", port);//machine name, port number
  File file = new File(fileName);
  // Get the size of the file
  long length = file.length();
  if (length &gt; Integer.MAX_VALUE) 
  {
      System.out.println("File is too large.");
  }
  byte[] bytes = new byte[(int) length];
  FileInputStream fis = new FileInputStream(file);
  BufferedInputStream bis = new BufferedInputStream(fis);
  BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
  
  int count;
  
  while ((count = bis.read(bytes)) &gt; 0) 
  {
      out.write(bytes, 0, count);
  }
  
  out.flush();
  out.close();
  fis.close();
  bis.close();
  socket.close();
  </code></pre>