-1

Code example:

         BufferedOutputStream bos = null
         bos = new BufferedOutputStream(new FileOutputStream("/Desktop/test.txt",true);

         byte[] dataPacket;
         dataPacket = new byte[]{-128, 10};

         bos.write(dataPacket, 0, dataPacket.length);

         bos.flush();
         bos.close();

Output display in test.txt: '\80' (not the output display desired. Want '[-128, 10]' to be displayed when opening output file).

chohmann
  • 27
  • 4
  • 2
    Does this answer your question? [how to write an array to a file Java](https://stackoverflow.com/questions/13707223/how-to-write-an-array-to-a-file-java) – Savior May 26 '20 at 14:40

1 Answers1

0

Answer:

Replace the line:

                      bos.write(dataPacket, 0, dataPacket.length); 

with the following:

                      String s = Arrays.toString(dataPacket);
                      bos.write(s.getBytes());

Now when opening test.txt, the displayed output will be [-128, 10].

chohmann
  • 27
  • 4