繁体   English   中英

将字节数组转换为字符串,再转换回字节数组

[英]Converting a byte array to a string back to a byte array

我目前正在做作业,并且有一个字节数组,可将其写入文件。 我希望能够通过做相反的事情来从文件中取出字节数组,但是我认为我没有得到正确的值。

这是写入文本文件的代码

PrintWriter fw = new PrintWriter(new BufferedWriter(new FileWriter("tc.txt",true)));
KeyPair keyPair = generateKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
fw.println(email +" " + publicKey + " " + privateKey); //adds the site into the text file whcih contains all the blacklisted sites
fw.flush();

我尝试以字符串形式返回信息,并使用.getBytes()将其转换回这样的字节数组

tempPublicKey = (blScanner.next()).getBytes();

这似乎不对,两者之间是否发生了错误的事情?

当您将任意字节序列写入文本文件时在读取和读取此文本文件时考虑在写入和解码时将字节编码为base64格式

文本文件不适合存储和检索任意字节序列,因为某些字节可能被识别为格式化字符等。

将字节编码为base64并将其解码回去将在写入和读取文本文件时保留字节序列。

暂无
暂无

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

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