BufferedWriter only works the first time
<p>I have a simple server-client program that takes info from the client and passes it to the server. The messages are passed through a DatagramSocket. The server then takes the message and writes it to a text file.</p>

<p>My initial message works fine(gets printed to the file). However, if I run the client again then the new message does not get written to the file. I have to RESTART the server for the message to be printed again.</p>

<p>I have an arrayCopy method that copies two arrays and puts it into a larger array.</p>

<p>CLIENT</p>

<pre><code>public static void main(String[] args) throws IOException {
    System.out.println("Enter Username");
    BufferedReader usernameInput = new BufferedReader(new InputStreamReader(System.in));

    // Get the Username
    String username = directoryInput.readLine();
    byte[] usrname = username.getBytes();

    //Copy Username to Array
    byte[] tempArray = copyarray(packetheader, usrname);

    buffer = tempArray;
    mypacket = new DatagramPacket(buffer, buffer.length, IPaddr, 40000);
    clientSocket = new DatagramSocket();
    clientSocket.send(mypacket);
</code></pre>

<p>Server</p>

<pre><code>public static void main(String args[]) throws Exception {
    String Database;
    textfile = "C:\\textfile.txt";
    DatagramSocket serverSock = new DatagramSocket(40000);

    byte[] rbuf = new byte[97];

    DatagramPacket recievedPacket = new DatagramPacket(rbuf, rbuf.length);
    serverSock.receive(recievedPacket);
    String byteToString = new String(recievedPacket.getData(), 0, recievedPacket.getLength(), "US- ASCII");
    FileWriter fstream = new FileWriter(textfile);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(byteToString);
    out.close
}
</code></pre>
