English 中文(简体)
使用缓冲输出输出输出Stream 以循环时写入文件的字节。 需要帮助
原标题:Using BufferedOutputStream to write bytes to file with while loop. Help needed

我试图将字节写入有缓冲输出的文本文件, 但我需要它来工作。 这意味着要与 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);
问题回答

重新处理完后,您不会关闭溪流 。

您是否缺少缓冲输出. flush () 来冲出所有缓冲数据 。

尝试关闭您的 boffered outputStream

添加 缓冲输出输出. close ();

如果您不关闭您的 DextputStream , 您可能会丢失缓冲数据, 这可能是你的情况。 要么关闭它, 要么冲掉它 。

您需要使用 boffered outputStream s flush () <() 方法, 才能将所有结果都写出来。 引用 close () 方法也可以执行 flush ()

http://www.javapractices.com/topic/TopicAction.do?Id=8" rel="nofollows">http://www.javapractices.com/topic/TopicAction.do?Id=8





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签