简体   繁体   English

使用PrintWriter将字节保存到txt文档中

[英]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. 我有一个要存储在txt文档中的字节,因此可以在无需重新创建该字节的情况下使用。

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? 根据我的经验, mybyte可能是byte[] ,对吗? 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("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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