我正在尝试使用BufferedOutputStream将字节写入文件,但是我需要在while循环中工作。这意味着可以与TFTP服务器一起使用。它写入绝对没有任何内容的文件(这毫无意义)。谁能帮我这个?

            WRQ WRQ = new WRQ();
            ACK ACK = new ACK();
            DatagramPacket outPacket;
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));
            byte[] bytes;
            byte[] fileOut;
            outPacket = WRQ.firstPacket(packet);
            socket.send(outPacket);

            socket.receive(packet);

            while (packet.getLength() == 516){

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

            socket.receive(packet);

            }

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

最佳答案

完成流操作后,您不会关闭流。

关于java - 使用BufferedOutputStream通过while循环将字节写入文件。需要帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10674665/

10-10 08:48