简体   繁体   English

从基于增量Java的加密文件中读取行

[英]Reading lines from a encrypted file based on a delimeter java

I have an file containing encrypted Json Strings, i am using AES encryption with padding, i have another class which reads these files as input and decrypts the encrypted Json Strings, the problem i am facing is the encrypted json string is being written on multiple lines like 我有一个包含加密的Json字符串的文件,我使用带填充的AES加密,我还有另一个类读取这些文件作为输入并解密加密的Json字符串,我面临的问题是加密的json字符串正在多行写入喜欢

TRX+s0CJYKyKnUJFrFpG77GRxQBDqj4diJhEEmP2sPO0RkgZNeR0acPftfj85ef7WA0ty078rPvk
l3GTb8blA9ytd/SXkaGbp37pLIZilSYoyHTMy/Dd+GiNommta/aqPd5v0CRPda4lKf3tQgGBAw==

i am using Java 8 to read string using File.lines(path), this is giving me only the first line of the encrypted string, instead i need the entire encrpted string to be read into a string object, is there a way to specify different delimiter for File.lines()?. 我正在使用Java 8使用File.lines(path)读取字符串,这只给了我加密字符串的第一行,相反,我需要将整个加密字符串读入字符串对象,有没有一种方法可以指定File.lines()的不同分隔符? Also please note that the actual encrypted string spans around 10 lines, and the file has large number of documents. 另外请注意,实际的加密字符串大约跨越10行,并且该文件包含大量文档。

在您的输出上尝试一下。

string.replaceAll(".*\\R.*", "");

Okay here's what i did, I wrote the encrypted strings with a delimiter "," and then used scanner to read from the file, the code goes as below 好的,这就是我的工作,我用定界符“”写了加密的字符串,然后使用扫描仪从文件中读取,代码如下

Scanner x = new Scanner(File);
x.setDelimeter(",");
while(x.hasNext()){
decrypt(x.next());
}

Answer 2: Use a different encoder which doesn't add new lines to the encrypted string, and use the existing File.readLine() method. 答案2:使用不会向加密字符串添加新行的其他编码器,并使用现有的File.readLine()方法。 i used commons io codec base64 encoder. 我使用了Commons io编解码器base64编码器。

The Files.lines(path) method itself does not allow for a different delimiter. Files.lines(path)方法本身不允许使用其他定界符。 If you want to retain a similar code style to what you currently have, I would read the file into a single string this way: 如果您想保留与当前类似的代码样式,则可以通过以下方式将文件读取为单个字符串:

String encrypted ="";
for (String line : Files.readAllLines(path)) encrypted += line;

This will fill the string with every line in turn, removing the newlines. 这将依次在每一行中填充字符串,并删除换行符。 If you want them to stay, add them back manually: encrypted += line + "\\n"; 如果要保留它们,请手动将其重新添加: encrypted += line + "\\n";

That being said, if your method of writing to a file is causing newlines to appear in the file, and you don't want any newlines in the file, the first thing you should re-evaluate is that output, rather than adjusting your method of reading. 话虽这么说,如果您写入文件的方法导致换行出现在文件中,并且您不希望文件中有任何换行,那么您应该重新评估的第一件事就是输出,而不是调整方法阅读。

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

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