0

My codes are

public void sendFile () throws IOException {
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        byte[] buffer = new byte[4096];
        FileInputStream fileInputStream = new FileInputStream("\"C:\\Users\\abdul\\Desktop\\Instructions.txt\"");
        int bytes = fileInputStream.read(buffer,0,buffer.length);
        dataOutputStream.write(buffer,0,bytes);
    }
private void receiveFile() throws Exception{
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        byte[] buffer = new byte[Integer.MAX_VALUE];
        int bytes = dataInputStream.read(buffer,0,buffer.length);
        FileOutputStream fileOutputStream = new FileOutputStream("Instructions.txt");
        fileOutputStream.write(buffer,0,bytes);
    }

I do not get an error but when I run my program, the output just spams null for the class using receiveFile. I just need to send a file from server to client.

  • This question looks like a duplicate of this https://stackoverflow.com/questions/9520911/java-sending-and-receiving-file-byte-over-sockets , did you check that answer ? – A. Lion Dec 08 '21 at 14:23
  • 1
    Why are you wrapping the streams in `DataOutputStream` and `DataInputStream`? It would make sense if you were using the `readFully` method… – Holger Dec 08 '21 at 18:01

0 Answers0