简体   繁体   中英

Saving a byte into a txt document using PrintWriter

I have a byte I want to store in a txt document so it can be used down the line w/o recreating the byte.

I can do this using plain strings, but when I try to save the byte it saves this:

[B@4de1eaed

Is there any other way I can encode the byte so I can load it back up again and it will read the byte?

Here is my code:

        public static byte[] tiles;
        PrintWriter writer = new PrintWriter("mytxtdocument.txt", "UTF-8");
        writer.println("BYTE: " + mybyte);
        writer.close();

If you want to save an array of bytes:

public static byte[] tiles;
//...
//here you add some tiles
//...
PrintWriter writer = new PrintWriter("mytxtdocument.txt", "UTF-8");
for(int i = 0; i < tiles.length; i++) {
        writer.println("BYTE: " + tiles[i]);
}
writer.close();

Based on my experience, mybyte is probably a byte[] , correct? In that case, you need to write your bytes one at a time.

writer.print("BYTE: ");
for (int i=0;i<mybyte.length;i++) {
  writer.print("" + mybyte[i] + " ");
}
writer.println("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM