我现在正在做一个任务,我有一个字节数组,我把它写到一个文件上。我希望能够通过执行与我所做的相反的操作来从文件中取回字节数组,但我认为我没有得到正确的值。
这是要写入文本文件的代码
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();
看起来不太对劲,是不是中间发生了什么错的事情?
发布于 2017-04-03 01:41:43
将任意字节序列写入文本文件时,在读取此文本文件时,请在写入和解码时考虑encoding your bytes to base64 format。
文本文件不适合存储和检索任意字节序列,因为某些字节可能被识别为格式化字符等。
将你的字节编码成base64,然后再解码回来,将会在写入和读取文本文件时保留你的字节序列。
https://stackoverflow.com/questions/43171389
复制相似问题