title: Copy files through sockets in Java
body: <p>I'm having troubles with files in Java.</p>
  
  <p>The issue is: I'm trying to copy a file (not only text, any kind) from one side of a socket to another. I read the file with a BufferedReader (byte by byte) and write them on a file with a FileOutputStream.</p>
  
  <p>It works fine, but when I open the file it <strong>is not the same as te original</strong>, it is writing the bytes on a text file or something like that.</p>
  
  <p>A piece of code:</p>
  
  <pre><code>        in = new BufferedInputStream(s.getInputStream());
          byte b[] = new byte[MAX_LENGTH];
          File f = new File(name);
          FileOutputStream fos = new FileOutputStream(name);
          for(int i = 0; i &lt; segments; i++){
              in.read(b,i*MAX_LENGTH,MAX_LENGTH);
              fos.write(b);
          }
  </code></pre>
  
  <p>Where s is a opened socket (working fine), name the name of the file and segments the number of segments sent through the socket (segments of MAX_LENGTH).</p>