2017-04-02 188 views
0

我正在做一个任务,我有一个字节数组,我写入文件。 我希望能够通过与我做的相反的方式将字节数组从文件中取出,但我认为我没有得到正确的值。将字节数组转换回字节数组返回字节数组

这是写入文本文件

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(); 

它似乎不是正确的,有什么事情发生在这是错误的?

+0

你给什么输入和你收到什么输出? – nhouser9

+1

将每个字节数组(不包含字节数组的内容)的toString()表示写入文件。将字节写入文件不是一种正确的方法。你无法读取和获取字节数组的内容。 –

回答

0

当你正在写任意字节序列到文本文件,考虑encoding your bytes to base64 format书写时与解码当你读这个文本文件。

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

编码的字节base64和解码回来时将保留您的字节序列写入和阅读文本文件。

相关问题