When I send files separated by packets, the new created file is empty
<p>I am making a client-server program, which separates the file into packets. When I send a file, and when I open it it's empty. My problem is that empty file, how can I correct the code, to work. I want to separate the file, and if something happen with one of the packets to send it from another PC. I'm not good at java, but I am trying to improve my skills. Here are the both codes:<br>
<strong>Server part:</strong></p>

<pre><code>import java.net.*;
import java.io.*;

public class PacketServer {
    public static void main(String[] args) throws IOException {
        // create socket
        ServerSocket servsock = new ServerSocket(29311);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // sendfile
            File myFile = new File("D://test.txt");
            System.out.println(myFile.length());

            byte[] mybytearray = new byte[(int) myFile.length()];

            PrintWriter output = new PrintWriter(sock.getOutputStream(), true);
            output.println(myFile.length());

            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);

            bis.read(mybytearray, 0, mybytearray.length);

            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");

            long now = System.currentTimeMillis();
            int obshto = (int) myFile.length();
            int b = obshto / 60;
            int c = obshto % 60;// ako file ne e kraen broi paketi ot 60 byte
            System.out.println(c);
            int packet;
            int gpacket;
            for (packet = 1; packet &lt;= b; packet++) {

                System.out.println("izprashta packet:" + packet
                        + " s golemina " + 60 * packet + " bit / "
                        + myFile.length() + " w moment: " + now);
                os.write(mybytearray, 0, 60);
                if ((c != 0) &amp;&amp; (b == packet)) {
                    gpacket = c;
                    System.out.println("izprashta packet:" + packet
                            + " s golemina " + (c) + " bit / "
                            + myFile.length() + " w moment: " + now);
                    os.write(mybytearray, 0, c);
                }
            }
            os.flush();
            sock.close();
        }
    }
}
</code></pre>

<p>and<br><strong>Client part:</strong></p>

<pre><code>import java.net.*;
import java.util.Scanner;
import java.io.*;

public class PacketClient {
    public static void main(String[] args) throws IOException {

        int filesize = 6022386;
        long start = System.currentTimeMillis();

        Socket sock = new Socket("127.0.0.1", 29311);
        System.out.println("Connecting...");
        Scanner input = new Scanner(sock.getInputStream());

        String response = input.nextLine();
        int Fsize = Integer.parseInt(response);

        // receive file
        byte[] mybytearray = new byte[filesize];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("D://Receive/test.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // obshto packets kam momenta
        int obshto = Fsize;
        int b = obshto / 60;
        int c = obshto % 60;
        int bytesRead;
        int current;

        for (b = obshto / 60; b &gt;= 1; b--) {

            bytesRead = is.read(mybytearray, (b * 60), 60);
            current = bytesRead;
            long now1 = System.currentTimeMillis();

            do {
                bytesRead = is.read(mybytearray, current,
                        (mybytearray.length - current));
                System.out.println("bytesRead " + bytesRead);

                if (bytesRead &lt;= Fsize)
                    current += bytesRead;

                System.out.println("bytesRead " + bytesRead);
            } while (bytesRead &gt; -1);
            System.out.println("poluchava packet:" + b + " s golemina" + 60 * b
                    + " bit / " + Fsize + " w moment: " + now1);

            bos.write(mybytearray, 0, 60);
            if ((c != 0) &amp;&amp; (b == 1)) {

                bytesRead = is.read(mybytearray, 0, c);
                current = bytesRead;
                long now = System.currentTimeMillis();

                do {
                    bytesRead = is.read(mybytearray, current,
                            (mybytearray.length - current));
                    // System.out.println("bytesRead " + bytesRead);

                    if (bytesRead &lt;= Fsize)
                        current += bytesRead;

                    // System.out.println("bytesRead "+ bytesRead);
                } while (bytesRead &gt; -1);
                System.out.println("poluchava packet:" + (b + 1)
                        + " s golemina" + c + " bit / " + Fsize + " w moment: "
                        + now);

                bos.write(mybytearray, 0, c);
            }

            bos.flush();

            long end = System.currentTimeMillis();
            System.out.println(end - start);

            bos.close();
            sock.close();
        }

    }
}
</code></pre>
